From 0947b64287f7dd39310b7da10c7d8675c090b230 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Tue, 25 Feb 2020 15:03:07 +0200 Subject: yep --- backend/src/authn.js | 13 +++---------- frontend/src/Activate.js | 15 ++++++++++----- frontend/src/App.js | 19 +++++++++++++++++-- frontend/src/Register.js | 6 +++++- 4 files changed, 35 insertions(+), 18 deletions(-) diff --git a/backend/src/authn.js b/backend/src/authn.js index 52ba862..78703bb 100644 --- a/backend/src/authn.js +++ b/backend/src/authn.js @@ -107,24 +107,19 @@ const registerRoute = async (req, res) => { const salt = shortid.generate(); const pwHash = sha512(salt + password); - const sessionToken = shortid.generate(); - const sessionTokenHash = sha512(salt + sessionToken); - const insertedUsers = await db('users').insert({ username, password: pwHash, email, salt }).returning('*'); const user = insertedUsers[0]; const activationToken = shortid.generate(); await db('tokens').insert({ user_id: user.id, type: 'activation', value: activationToken }); - // await db('tokens').insert({ user_id: user.id, type: 'session', value: sessionTokenHash }); - // res.set('Set-Cookie', buildSessionCookie({ username, sessionToken, remember: false, isLogout: false })); - await consoleBackend.send({ recipients: [ username ], subject: `Welcome to the service, ${username}!`, body: `Welcome.\n\nActivate your account at http://localhost:3000/activate/${activationToken}.\n\nBR,\nTeam`, }); + res.set('Set-Cookie', buildSessionCookie({ isLogout: true })); return res.json({ username, }); @@ -142,7 +137,7 @@ const loginRoute = async (req, res) => { const user = await db('users').where({ username }).first(); if (!user) { res.status(400); - res.json({ + return res.json({ error: 'Wrong username or password', }); } @@ -152,16 +147,14 @@ const loginRoute = async (req, res) => { if (pwHash !== user.password) { res.status(400); - res.json({ + return res.json({ error: 'Wrong username or password', }); } const sessionToken = shortid.generate(); const sessionTokenHash = sha512(user.salt + sessionToken); - await db('tokens').insert({ user_id: user.id, type: 'session', value: sessionTokenHash }); - res.set('Set-Cookie', buildSessionCookie({ username, sessionToken, isLogout: false })); return res.json({ diff --git a/frontend/src/Activate.js b/frontend/src/Activate.js index 8a420f4..17d980e 100644 --- a/frontend/src/Activate.js +++ b/frontend/src/Activate.js @@ -2,14 +2,19 @@ import React, { useEffect, useState } from 'react'; import axios from './axios'; import { Link } from '@reach/router'; -const Activate = ({ activationToken }) => { +import userState from './UserState'; + +const Activate = ({ user, activationToken }) => { const [ data, setData ] = useState({}); useEffect(() => { (async () => { try { - await axios.post('/activate', { activationToken }); + const resp = await axios.post('/activate', { activationToken }); + const { username } = resp.data; + user.authError = null; + user.username = username; setData({ - message: 'Account succesfully activated. You can now log in.', + message: 'Account succesfully activated. You are now logged in.', }); } catch (err) { setData({ @@ -27,10 +32,10 @@ const Activate = ({ activationToken }) => { {data.error}
{data.message}
- {data.message && To login page} + {data.message && To front page} ); }; -export default Activate; +export default props => ; diff --git a/frontend/src/App.js b/frontend/src/App.js index 38d8aef..51ba18c 100644 --- a/frontend/src/App.js +++ b/frontend/src/App.js @@ -9,7 +9,7 @@ import './App.css'; import axios from './axios'; import Activate from './Activate'; -const App = () => { +const LoggedInRoutes = props => { useEffect(() => { const fetchUserInfo = async () => { try { @@ -24,8 +24,23 @@ const App = () => { fetchUserInfo(); }, []); + return props.children; +}; + +const App = () => { + const fetchUserInfo = async () => { + try { + const resp = await axios.get('/user'); + const { username } = resp.data; + userState.username = username; + } catch (err) { + userState.authError = err; + console.error(err); + } + }; + fetchUserInfo(); return ( -
+
diff --git a/frontend/src/Register.js b/frontend/src/Register.js index 97a5992..c2d7e98 100644 --- a/frontend/src/Register.js +++ b/frontend/src/Register.js @@ -18,7 +18,7 @@ const Register = () => { const { username } = resp.data; userState.authError = null; userState.username = username; - navigate('/'); + setData({ submitted: true }); } catch (err) { if (err.response && err.response.data) { setData({ @@ -31,6 +31,10 @@ const Register = () => { } }; + if (data.submitted) { + return

You should be receiving an account activation email shortly.

; + } + return (

Register new account

-- cgit v1.3