-
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.
Merge pull request #36 from LukasMartini/login
Login system
- Loading branch information
Showing
12 changed files
with
366 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
PREPARE createUser(text, text, text, text, timestamp, text) AS | ||
INSERT INTO users(username, email, password_hash, created_at, token, expiry_date, salt) VALUES($1, $2, $3, NOW(), $4, $5, $6); | ||
|
||
PREPARE login(text) AS | ||
SELECT salt, password_hash, token, username, email, expiry_date > NOW() FROM users | ||
WHERE email = $1 OR username = $1; | ||
|
||
PREPARE renewToken(text, text, timestamp) AS | ||
UPDATE users | ||
SET token = $2, expiry_date = $3 | ||
WHERE email = $1 OR username = $1; | ||
|
||
PREPARE authorize(text) AS | ||
SELECT id FROM users | ||
WHERE token = $1 AND expiry_date > NOW() |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
'use client'; | ||
|
||
import { Button } from '@/components/ui/button'; | ||
import { useAuth } from '@/components/auth/AuthContext'; | ||
|
||
const LoginPage = () => { | ||
const user = useAuth(); | ||
|
||
const handleSubmit = async (event: any) => { | ||
event.preventDefault(); | ||
const formData = new FormData(event.target); | ||
console.log(formData); | ||
const response = await fetch(`http://localhost:5001/api/login`, { | ||
method: "POST", | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify(Object.fromEntries(formData)) | ||
}); | ||
|
||
const result = await response.json(); | ||
|
||
if (result.success) { | ||
user.login(result.token, result.email, result.username); | ||
} | ||
}; | ||
console.log(user.auth); | ||
|
||
return ( | ||
<div className="bg-[#2C2C2C] text-white px-64"> | ||
{user.auth.token && ( | ||
<div> | ||
<h1 className="text-xl text-center font-bold pt-8"> | ||
Logged in! | ||
</h1> | ||
|
||
<Button className="w-full" onClick={()=>user.logout()}>Logout</Button> | ||
</div> | ||
)} | ||
{!user.auth.token && ( | ||
<div> | ||
<h1 className="text-xl text-center font-bold pt-8"> | ||
Login to PokerReplay | ||
</h1> | ||
<br/><br/> | ||
<form encType='multipart/form-data' onSubmit={handleSubmit} className="mx-auto max-w-xs p-8 space-y-4 bg-[#232323] rounded-lg shadow-md"> | ||
<input | ||
className="w-full bg-gray-800 rounded-md border border-gray-400 py-2 px-4 text-sm text-white placeholder-gray-400" | ||
type="text" | ||
name="username" | ||
placeholder="Email or username" | ||
/> | ||
<input | ||
className="w-full bg-gray-800 rounded-md border border-gray-400 py-2 px-4 text-sm text-white placeholder-gray-400" | ||
type="password" | ||
name="password" | ||
placeholder="Password" | ||
/> | ||
<Button className="w-full" type='submit'>Login</Button> | ||
</form> | ||
</div> | ||
)} | ||
</div> | ||
) | ||
} | ||
|
||
export default LoginPage |
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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
'use client'; | ||
|
||
import { Button } from '@/components/ui/button'; | ||
|
||
const handleSubmit = async (event: any) => { | ||
event.preventDefault(); | ||
const formData = new FormData(event.target); | ||
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/; | ||
const passwordRegex = /^(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_\-\+=~`{}$begin:math:display$$end:math:display$|\\:;"'<,>.?/]).{12,}$/; | ||
|
||
if (!emailRegex.test(formData.get("email") as string)) { | ||
alert("Please enter a valid email address ([email protected])"); | ||
return; | ||
} | ||
if (!passwordRegex.test(formData.get("password") as string)) { | ||
alert("Please enter a minimum 12 character password including a number and special character."); | ||
return; | ||
} | ||
if (formData.get("password") != formData.get("confirm_password")) { | ||
alert("Confirm Password doesn't match your password"); | ||
return; | ||
} | ||
|
||
|
||
const response = await fetch(`http://localhost:5001/api/signup`, { | ||
method: "POST", | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify(Object.fromEntries(formData)) | ||
}); | ||
|
||
window.location.reload(); | ||
|
||
console.log(await response.json()); | ||
}; | ||
|
||
const SignupPage = () => { | ||
return ( | ||
<div className="bg-[#2C2C2C] text-white px-64"> | ||
<h1 className="text-xl text-center font-bold pt-8"> | ||
Sign up for PokerReplay | ||
</h1> | ||
<br/><br/> | ||
<form encType='multipart/form-data' onSubmit={handleSubmit} className="mx-auto max-w-xs p-8 space-y-4 bg-[#232323] rounded-lg shadow-md"> | ||
<input | ||
className="w-full bg-gray-800 rounded-md border border-gray-400 py-2 px-4 text-sm text-white placeholder-gray-400" | ||
type="email" | ||
name="email" | ||
placeholder="Email address" | ||
/> | ||
<input | ||
className="w-full bg-gray-800 rounded-md border border-gray-400 py-2 px-4 text-sm text-white placeholder-gray-400" | ||
type="text" | ||
name="username" | ||
placeholder="Username" | ||
/> | ||
<input | ||
className="w-full bg-gray-800 rounded-md border border-gray-400 py-2 px-4 text-sm text-white placeholder-gray-400" | ||
type="password" | ||
name="password" | ||
placeholder="Password" | ||
/> | ||
<input | ||
className="w-full bg-gray-800 rounded-md border border-gray-400 py-2 px-4 text-sm text-white placeholder-gray-400" | ||
type="password" | ||
name="confirm_password" | ||
placeholder="Confirm Password" | ||
/> | ||
<Button className="w-full" type='submit'>Sign Up</Button> | ||
</form> | ||
</div> | ||
) | ||
} | ||
|
||
export default SignupPage |
Oops, something went wrong.