-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
119 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,11 +11,11 @@ export default NextAuth({ | |
}, | ||
async authorize(credentials, req) { | ||
// Handle the case when 'credentials' is undefined | ||
if (!credentials) { | ||
/* if (!credentials) { | ||
throw new Error("Invalid credentials"); | ||
} | ||
} */ | ||
// Mock user lookup logic for testing | ||
const mockUser = { | ||
/* const mockUser = { | ||
id: 1, | ||
name: "John Smith", | ||
email: "[email protected]", | ||
|
@@ -29,18 +29,32 @@ export default NextAuth({ | |
return mockUser; | ||
} else { | ||
throw new Error("Invalid credentials"); | ||
} | ||
} */ | ||
|
||
extract_user = response => { | ||
|
||
console.log("message!"); | ||
let response_body_obj = JSON.parse(response.payload); // Turn the response body JSON into an object | ||
|
||
let access_jwt_str = response_body_obj?.access; // Get the access token (JWT) from body object | ||
|
||
let jwt_payload_base64 = access_jwt_str?.split('.')[1]; // Split the jwt at every '.' and use first index to get Base64 encoded payload (JWT => header.payload.signature => 0.1.2) | ||
|
||
let jwt_payload = atob(jwt_payload_base64); // Decode the Base64 encoded payload | ||
|
||
let user = JSON.parse(jwt_payload); // Turn the decoded payload JSON string into the an object | ||
|
||
return user; | ||
|
||
} | ||
|
||
const res = await fetch("http://127.0.0.1:8000/api/signIn", { | ||
const res = await fetch("http://178.128.192.215:8000/api/signIn", { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
username: credentials?.username, | ||
password: credentials?.password, | ||
body: JSON.stringify({ //constructs the request body | ||
email: credentials?.username, //data to be sent in the request body | ||
password: credentials?.password, //data to be sent in the request body | ||
}), | ||
}); | ||
const user = await res.json(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,28 +3,63 @@ import Button from "../components/Button"; | |
import TextBox from "../components/TextBox"; | ||
import { signIn } from "next-auth/react"; | ||
import React, { useRef } from "react"; | ||
import styles from '../styles/Signin.module.css' | ||
|
||
const LoginPage = () => { | ||
const userName = useRef(""); | ||
const pass = useRef(""); | ||
|
||
const onSubmit = async () => { | ||
/* const onSubmit = async () => { | ||
const result = await signIn("credentials", { | ||
username: userName.current, | ||
email: userName.current, | ||
password: pass.current, | ||
redirect: true, | ||
callbackUrl: "/dashboard", | ||
}); | ||
}; */ | ||
|
||
const onSubmit = async () => { | ||
console.log("Login Pressed!"); | ||
const res = await fetch("http://178.128.192.215:80/api/signIn/", { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
email: userName.current, //username entered by the user in textbox | ||
password: pass.current, //password entered by the user in textbox | ||
}), //"email": "[email protected]", password: "fr" | ||
}); | ||
console.log("Email: " + userName.current); | ||
const response_body = await res.json(); // Parse the response body as JSON | ||
if (res.ok) { | ||
const user = extract_user(response_body); // Call the extract_user function with the response body | ||
window.location.href = "/dashboard"; | ||
localStorage.setItem('user', JSON.stringify(user)); | ||
return user; | ||
} else { | ||
console.log("Response not OK!"); | ||
return null; | ||
} | ||
}; | ||
|
||
const extract_user = (response) => { | ||
let access_jwt_str = response?.access; | ||
let jwt_payload_base64 = access_jwt_str?.split('.')[1]; | ||
let jwt_payload = Buffer.from(jwt_payload_base64, 'base64'); //equivalent to atob | ||
let user = JSON.parse(jwt_payload); | ||
return user; | ||
}; | ||
|
||
return ( | ||
<div | ||
className={ | ||
"flex flex-col justify-center items-center h-screen bg-gradient-to-br gap-1 from-cyan-300 to-sky-600" | ||
} | ||
> | ||
<div className="px-7 py-4 shadow bg-white rounded-md flex flex-col gap-2"> | ||
<TextBox lableText="User Name" onChange={(e) => (userName.current = e.target.value)} /> | ||
<TextBox | ||
> <h1 className={styles.signin_header}>Sign in</h1> | ||
<div className="{styles.signinnew} px-7 py-4 shadow bg-white rounded-md flex flex-col gap-2"> | ||
<TextBox className={styles['form-group']} lableText="Email" onChange={(e) => (userName.current = e.target.value)} /> | ||
<TextBox className={styles['form-group']} | ||
lableText="Password" | ||
type={"password"} | ||
onChange={(e) => (pass.current = e.target.value)} | ||
|