-
Notifications
You must be signed in to change notification settings - Fork 11
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
added login/signup page with basic functionality #27
Changes from 1 commit
a30e1dd
92d15ea
6fef875
ddb5427
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import nextConnect from 'next-connect' | ||
import bcrypt from 'bcrypt' | ||
import { query } from '../../utils/query' | ||
|
||
const handler = nextConnect() | ||
|
||
.post(async (req, res) => { | ||
const { email, password } = req.body | ||
|
||
if (!email || !password) res.status(400).json({ error: 'Email or Password cannot be blank' }) | ||
|
||
try { | ||
const results = await query(` | ||
SELECT password FROM users WHERE email = ? | ||
`, email) | ||
|
||
if (!results[0]) return res.status(401).json({ error: 'Email not found!' }) | ||
|
||
const valid = await bcrypt.compare(password, results[0].password) | ||
|
||
if (valid) { | ||
/* | ||
todo: | ||
- create a session for the user, store it in db | ||
- store session in cookie/localstorage/sessionstorage | ||
- check session with server upon refresh | ||
- two factor authentication ? | ||
*/ | ||
return res.status(200).json({ message: 'Success!' }) | ||
} | ||
|
||
else if (!valid) return res.status(401).json({ error: 'Incorrect Password!' }) | ||
|
||
} catch (e) { | ||
res.status(500).json({ error: e.message }) | ||
} | ||
}) | ||
|
||
export default handler |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import nextConnect from 'next-connect' | ||
import bcrypt from 'bcrypt' | ||
import { query } from '../../utils/query' | ||
|
||
const handler = nextConnect() | ||
|
||
.post(async (req, res) => { | ||
const { email, password } = await req.body | ||
|
||
if (!email || !password) res.json({ error: 'Email or Password cannot be blank!' }) | ||
|
||
try { | ||
|
||
/* | ||
todo: | ||
- validate password: see if it has 8 digits and 1 symbol | ||
- add check to see if email has @uwinnipeg.ca or @webmail.uwinnipeg.ca | ||
- add check to see if email exists | ||
- send validation link to email | ||
*/ | ||
|
||
const hashedPW = await bcrypt.hash(password, 10) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The salt rounds should probably be a system constant somewhere, in case we decide to change it later for some reason. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added now |
||
const results = await query(` | ||
INSERT INTO users(email, password) VALUES(?, ?) | ||
`, [email, hashedPW]) | ||
|
||
return res.status(200).json({ message: 'Registered!' }) | ||
} catch (e) { | ||
res.status(500).json({ message: e.message }) | ||
} | ||
}) | ||
|
||
export default handler |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import React, { useState } from 'react' | ||
import styles from '../styles/auth.module.css' | ||
import { useRouter } from 'next/router' | ||
|
||
export default function signin() { | ||
const Router = useRouter() | ||
const [response, setResponse] = useState() | ||
|
||
async function onSubmit(e) { | ||
e.preventDefault() | ||
|
||
const body = { | ||
email: e.currentTarget.email.value, | ||
password: e.currentTarget.password.value, | ||
} | ||
|
||
let res = await fetch('./api/signin', { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify(body), | ||
}) | ||
|
||
if (res.status===200) { | ||
// store session in cookie/localstorage/sessionstorage | ||
Router.push('/') | ||
} else { | ||
res = await res.json() | ||
setResponse(res) | ||
} | ||
} | ||
|
||
return ( | ||
<div className={[styles.auth, "noselect"].join(' ')}> {/* hacky way to add multiple classes */} | ||
|
||
<h1>Sign In</h1> | ||
|
||
{response && response.message && <p style={{color: "green"}}>{response.message}</p>} | ||
{response && response.error && <p style={{color: "red"}}>{response.error}</p>} | ||
|
||
{/* could use Formik or another library in the future */} | ||
<form className={styles.form} onSubmit={e => onSubmit(e)} > | ||
<input className={styles.field} name="email" placeholder="Email"/> | ||
<input className={styles.field} type="password" name="password" placeholder="Password"/> | ||
<button className={styles.submit} type="submit">Sign In</button> | ||
</form> | ||
|
||
</div> | ||
) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import React, { useState } from 'react' | ||
import styles from '../styles/auth.module.css' | ||
|
||
export default function signup() { | ||
const [response, setResponse] = useState() | ||
|
||
async function onSubmit(e) { | ||
e.preventDefault() | ||
|
||
const pass1 = e.currentTarget.password1.value | ||
const pass2 = e.currentTarget.password2.value | ||
|
||
if (pass1!==pass2) { | ||
setResponse({ error: 'Passwords do not match!' }) | ||
return | ||
} else if (!pass1 || !pass2) { | ||
setResponse({ error: 'Please enter and confirm a password.' }) | ||
return | ||
} | ||
|
||
const body = { | ||
email: e.currentTarget.email.value, | ||
password: pass1, | ||
} | ||
|
||
let res = await fetch('./api/signup', { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify(body), | ||
}) | ||
|
||
res = await res.json() | ||
setResponse(res) | ||
} | ||
|
||
|
||
|
||
return ( | ||
<div className={[styles.auth, "noselect"].join(' ')}> {/* hacky way to add multiple classes */} | ||
|
||
<h1>Sign Up</h1> | ||
|
||
{response && response.message && <p className={styles.submitMessage} style={{color: "green"}}>{response.message}</p>} | ||
{response && response.error && <p className={styles.submitMessage} style={{color: "red"}}>{response.error}</p>} | ||
|
||
{/* could use Formik or another library in the future */} | ||
<form className={styles.form} onSubmit={e => onSubmit(e)}> | ||
<input className={styles.field} name="email" placeholder="Email"/> | ||
<input className={styles.field} type="password" name="password1" placeholder="Password"/> | ||
<input className={styles.field} type="password" name="password2" placeholder="Confirm Password"/> | ||
<button className={styles.submit} type="submit">Sign Up</button> | ||
</form> | ||
|
||
</div> | ||
) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
.form, .auth { | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
} | ||
|
||
.auth { | ||
gap: 2rem; | ||
border-style: none; | ||
padding: 3rem 3rem; | ||
background-color: #fff; | ||
border-radius: 1.5rem; | ||
box-shadow: 0 1rem 2rem rgba(0,0,0,0.25), | ||
0 0.75rem 0.75rem rgba(0,0,0,0.22); | ||
position: absolute; | ||
top: 50%; | ||
left: 50%; | ||
transform: translate(-50%,-50%); | ||
} | ||
|
||
.auth h1 { | ||
margin: 0; | ||
pointer-events: none; | ||
} | ||
|
||
.auth p { | ||
font-size: 14px; | ||
line-height: 20px; | ||
letter-spacing: 0.5px; | ||
margin: 0; | ||
} | ||
|
||
.auth input { | ||
font-size: 14px; | ||
background-color: #eee; | ||
border: none; | ||
padding: 1rem 1.5rem; | ||
} | ||
|
||
.form { | ||
font-size: 16px; | ||
gap: 1rem; | ||
width: fit-content; | ||
height: fit-content; | ||
} | ||
|
||
.submit { | ||
margin-top: 1rem; | ||
border-radius: 1.25rem; | ||
border: 1px solid #FF4B2B; | ||
background-color: #FF4B2B; | ||
color: #FFFFFF; | ||
font-size: 12px; | ||
font-weight: bold; | ||
padding: 0.75rem 2.8125rem; | ||
letter-spacing: 1px; | ||
text-transform: uppercase; | ||
cursor: pointer; | ||
transition: transform 80ms ease-in; | ||
} | ||
|
||
.submit:active { | ||
transform: scale(0.95); | ||
} | ||
|
||
.submit:focus { | ||
outline: none; | ||
} | ||
|
||
.submit:hover { | ||
transform: scale(1.1); | ||
} | ||
|
||
.submit.ghost { | ||
background-color: transparent; | ||
border-color: #FFFFFF; | ||
} | ||
|
||
@media only screen and (max-width: 600px) { | ||
.form { | ||
max-width: 75vw; | ||
max-height: 50vh; | ||
overflow: auto; | ||
} | ||
|
||
.auth input { | ||
padding: 0.75rem; | ||
max-width: 50vw; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/*to disable text selection highlighting*/ | ||
.noselect { | ||
-webkit-touch-callout: none; /* iOS Safari */ | ||
-webkit-user-select: none; /* Safari */ | ||
-khtml-user-select: none; /* Konqueror HTML */ | ||
-moz-user-select: none; /* Old versions of Firefox */ | ||
-ms-user-select: none; /* Internet Explorer/Edge */ | ||
user-select: none; /* Non-prefixed version, currently | ||
supported by Chrome, Edge, Opera and Firefox */ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require('dotenv').config(); //Load from the .env file | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is loaded from the new server.js file, we don't need this anymore. |
||
const mysql = require('mysql2'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe import mysql2/promise so we can use async/await with the pools/queries |
||
|
||
export const pool = mysql.createPool({ | ||
host: process.env.DB_HOST, | ||
user: process.env.DB_USER, | ||
password: process.env.DB_PASS, | ||
database: process.env.DB_DATABASE, | ||
waitForConnections: true, | ||
connectionLimit: 10 | ||
}) | ||
|
||
export async function query(q, values) { | ||
try { | ||
const results = await pool.query(q, values) | ||
await pool.end() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We actually don't want to ever close this pool. It should always be available for the lifecycle of the server. |
||
return results | ||
} catch (e) { | ||
throw Error(e.message) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prepared Statements have to pass in their fields in an array, so you'd have to wrap email in an array.
[email]