-
Notifications
You must be signed in to change notification settings - Fork 12
added login/signup page with basic functionality #27
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a30e1dd
added login/signup page with basic functionality
Raghuboi 92d15ea
fixed query function
Raghuboi 6fef875
added salt rounds constant, updated auth api-routes, fixed issue with…
Raghuboi ddb5427
created systemconstants.js file, added SALT_ROUNDS to it
Raghuboi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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,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 |
This file contains hidden or 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,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) | ||
| 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 | ||
This file contains hidden or 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,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> | ||
| ) | ||
| } | ||
|
|
This file contains hidden or 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,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> | ||
| ) | ||
| } | ||
|
|
This file contains hidden or 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,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; | ||
| } | ||
| } |
This file contains hidden or 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,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 */ | ||
| } |
This file contains hidden or 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,19 @@ | ||
| const mysql = require('mysql2/promise'); | ||
|
|
||
| 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) | ||
| return results | ||
| } catch (e) { | ||
| throw Error(e.message) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
added now