Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2 - Auth Page #2

Open
wants to merge 2 commits into
base: 1-context-api
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.js → context/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useState, createContext } from "react";

export const Context = createContext();

export const ContextProvider = () => {
export const ContextProvider = (props) => {
const [username, setUsername] = useState("");
const [secret, setSecret] = useState("");

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"start": "next start"
},
"dependencies": {
"axios": "^0.23.0",
"next": "^11.0.0",
"react": "17.0.2",
"react-dom": "17.0.2"
Expand Down
66 changes: 62 additions & 4 deletions pages/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,63 @@
import React from "react";
import React, { useContext } from "react";

export default function Auth() {
return <div className="background">auth</div>;
}
import { Context } from "../context";

import { useRouter } from "next/router";

import axios from "axios";

const Auth = () => {
const { username, setUsername, secret, setSecret } = useContext(Context);

const router = useRouter();

function onSubmit(e) {
e.preventDefault();

if (username.length === 1 || secret.length === 1) return;

axios
.put(
"https://api.chatengine.io/users/",
{ username, secret },
{ headers: { "Private-Key": "c2f82e63-9978-4c5c-9c17-8b0dec845dc6" } }
)

.then((r) => {
router.push("/chats");
});
}

return (
<div className="background">
<div className="auth-container">
<form className="auth-form" onSubmit={(e) => onSubmit(e)}>
<div className="auth-title">NextJS Chat</div>

<div className="input-container">
<input
placeholder="Email"
className="text-input"
onChange={(e) => setUsername(e.target.value)}
/>
</div>

<div className="input-container">
<input
type="password"
placeholder="Password"
className="text-input"
onChange={(e) => setSecret(e.target.value)}
/>
</div>

<button type="submit" className="submit-button">
Login / Sign Up
</button>
</form>
</div>
</div>
);
};

export default Auth;
Loading