From 5ddd01a537999e3c510a823d0f687531f3ba6317 Mon Sep 17 00:00:00 2001 From: Mikayla Dobson <93477693+innocuous-symmetry@users.noreply.github.com> Date: Mon, 21 Nov 2022 17:24:44 -0600 Subject: [PATCH] troubleshooting login/logout --- client/src/App.tsx | 7 ++ client/src/components/pages/Login.tsx | 8 +- client/src/components/pages/Welcome.tsx | 13 +-- client/src/components/ui/Navbar.tsx | 30 ++++--- client/src/context/AuthContext.tsx | 15 ++++ client/src/sass/helpers/_placeholders.scss | 6 ++ client/src/util/{apiUtils.ts => apiUtils.tsx} | 18 +++- server/auth/index.ts | 21 ++++- server/loaders/express.ts | 6 ++ server/loaders/passport.ts | 9 +- server/models/user.ts | 8 +- server/package-lock.json | 82 ++++++++++++++----- server/package.json | 2 + server/routes/auth.ts | 32 +++++++- server/routes/index.ts | 4 - 15 files changed, 205 insertions(+), 56 deletions(-) create mode 100644 client/src/context/AuthContext.tsx rename client/src/util/{apiUtils.ts => apiUtils.tsx} (73%) diff --git a/client/src/App.tsx b/client/src/App.tsx index 3eb3740..8197c95 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -1,3 +1,4 @@ +import { useEffect } from 'react'; import { BrowserRouter, Routes, Route } from 'react-router-dom'; import Browser from './components/pages/Browser'; import Collection from './components/pages/Collection'; @@ -6,9 +7,15 @@ import Profile from './components/pages/Profile'; import Recipe from './components/pages/Recipe'; import Register from './components/pages/Register'; import Welcome from './components/pages/Welcome'; +import { useAuthContext } from './context/AuthContext'; import './sass/App.scss' function App() { + const authContext = useAuthContext(); + + useEffect(() => { + console.log(fetch("http://localhost:8080/auth").then(res => res.json()).then(x => console.log(x))); + }) return ( diff --git a/client/src/components/pages/Login.tsx b/client/src/components/pages/Login.tsx index 95c4e12..ebf186d 100644 --- a/client/src/components/pages/Login.tsx +++ b/client/src/components/pages/Login.tsx @@ -1,10 +1,15 @@ import { useCallback, useEffect, useState } from "react"; +import { useNavigate } from "react-router-dom"; +import { useAuthContext } from "../../context/AuthContext"; import { IUserAuth } from "../../schemas"; import { attemptLogin } from "../../util/apiUtils"; import { Button, Page, Panel } from "../ui"; import Form, { FormConfig } from "../ui/Form"; export default function Login() { + const authContext = useAuthContext(); + const navigate = useNavigate(); + const [form, setForm] = useState(); const [input, setInput] = useState({ email: '', @@ -18,7 +23,8 @@ export default function Login() { const handleLogin = async () => { if (!input.email || !input.password) return; const result = await attemptLogin(input); - console.log(result); + authContext.user = result; + navigate('/'); } const formConfig: FormConfig = { diff --git a/client/src/components/pages/Welcome.tsx b/client/src/components/pages/Welcome.tsx index 33b156c..fbaa04e 100644 --- a/client/src/components/pages/Welcome.tsx +++ b/client/src/components/pages/Welcome.tsx @@ -1,17 +1,18 @@ import { useState } from "react"; import { useNavigate } from "react-router-dom"; +import { useAuthContext } from "../../context/AuthContext"; import { Button, Page, Panel } from "../ui" import Divider from "../ui/Divider"; const Welcome = () => { - const [authorized, setAuthorized] = useState(false); + const authContext = useAuthContext(); const navigate = useNavigate(); const authUserActions = ( - - - - + + + + ) @@ -42,7 +43,7 @@ const Welcome = () => { - { authorized ? authUserActions : callToRegister } + { authContext.user ? authUserActions : callToRegister } ) } diff --git a/client/src/components/ui/Navbar.tsx b/client/src/components/ui/Navbar.tsx index 4c5967f..4f13cf6 100644 --- a/client/src/components/ui/Navbar.tsx +++ b/client/src/components/ui/Navbar.tsx @@ -1,22 +1,24 @@ import { useState } from "react"; +import { useNavigate } from "react-router-dom"; +import { useAuthContext } from "../../context/AuthContext"; +import { attemptLogout } from "../../util/apiUtils"; +import Button from "./Button"; import "/src/sass/components/Navbar.scss"; const Navbar = () => { - // state will be evaluated here to determine which navbar - // variant will be displayed - - // this will come from state (session?) + const authContext = useAuthContext(); + const navigate = useNavigate(); const [user, setUser] = useState('Mikayla'); const navbarLoggedIn = ( ) @@ -24,10 +26,10 @@ const Navbar = () => { const navbarNotLoggedIn = ( ) @@ -35,15 +37,19 @@ const Navbar = () => { const navbarRegistering = ( ) - return navbarLoggedIn; + if (authContext.user) { + return navbarLoggedIn; + } else { + return navbarNotLoggedIn; + } } export default Navbar; \ No newline at end of file diff --git a/client/src/context/AuthContext.tsx b/client/src/context/AuthContext.tsx new file mode 100644 index 0000000..0f67744 --- /dev/null +++ b/client/src/context/AuthContext.tsx @@ -0,0 +1,15 @@ +import { createContext, useContext } from "react"; +import { IUser } from "../schemas"; + + +interface IAuthContext { + user?: IUser +} + +const defaultValue: IAuthContext = { + user: undefined, +} + +export const AuthContext = createContext(defaultValue); + +export const useAuthContext = () => useContext(AuthContext); diff --git a/client/src/sass/helpers/_placeholders.scss b/client/src/sass/helpers/_placeholders.scss index 0b793d4..75e42b8 100644 --- a/client/src/sass/helpers/_placeholders.scss +++ b/client/src/sass/helpers/_placeholders.scss @@ -18,4 +18,10 @@ width: 45vw; } } + + &.flexrow { + display: flex; + flex-flow: row wrap; + justify-content: center; + } } \ No newline at end of file diff --git a/client/src/util/apiUtils.ts b/client/src/util/apiUtils.tsx similarity index 73% rename from client/src/util/apiUtils.ts rename to client/src/util/apiUtils.tsx index e8ed47a..ad106e0 100644 --- a/client/src/util/apiUtils.ts +++ b/client/src/util/apiUtils.tsx @@ -1,9 +1,17 @@ import { IUser, IUserAuth } from "../schemas"; const API = import.meta.env.APISTRING || "http://localhost:8080/"; +export const getBaseAPI = async () => { + return fetch(API); +} + +export const getCookies = async () => { + return fetch(API + 'auth'); +} + // auth handlers export const attemptLogin = async (data: IUserAuth) => { - const result: Array | null = await fetch(API + 'auth/login/', { + const success = await fetch(API + 'auth/login/', { method: "POST", headers: { "Content-Type": "application/json" @@ -11,7 +19,13 @@ export const attemptLogin = async (data: IUserAuth) => { body: JSON.stringify(data) }).then(response => response.json()); - return result; + if (success) return success; + return null; +} + +export const attemptLogout = async () => { + const response = await fetch(API + 'auth').then(response => response.json()); + return response; } export const attemptRegister = async (data: IUser) => { diff --git a/server/auth/index.ts b/server/auth/index.ts index d8c86ff..9c46c19 100644 --- a/server/auth/index.ts +++ b/server/auth/index.ts @@ -39,8 +39,25 @@ export default class AuthService { } - async login(data: IUserAuth): Promise> { - return []; + async login(data: IUserAuth) { + const { email, password } = data; + + try { + const user = await UserInstance.getOneByEmail(email); + if (!user) return { ok: false, user: null } + const match = await bcrypt.compare(password, user.password); + console.log(match); + return { + ok: match, + user: match ? user : null + } + } catch (e: any) { + throw new Error(e); + } + } + + async logout() { + } // methods for Google OAuth diff --git a/server/loaders/express.ts b/server/loaders/express.ts index b553c0a..7d69294 100644 --- a/server/loaders/express.ts +++ b/server/loaders/express.ts @@ -1,5 +1,6 @@ import { Express } from 'express'; import bodyParser from 'body-parser'; +import cookieParser from 'cookie-parser'; import morgan from 'morgan'; import cors from 'cors'; import session from 'express-session'; @@ -9,9 +10,14 @@ export const expressLoader = async (app: Express) => { app.use(cors()); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); + app.use(cookieParser()); app.use(morgan('tiny')); + app.get('/', (req, res) => { + res.cookie('name', 'express').send('cookie set'); + }) + app.use(session({ secret: process.env.SESSIONSECRET || "", cookie: { diff --git a/server/loaders/passport.ts b/server/loaders/passport.ts index cbec2f9..26dfca3 100644 --- a/server/loaders/passport.ts +++ b/server/loaders/passport.ts @@ -10,7 +10,9 @@ export const passportLoader = async (app: Express) => { app.use(passport.session()); passport.serializeUser((user, done) => { - done(null, user); + process.nextTick(() => { + done(null, user); + }) }) passport.deserializeUser((user: IUserAuth, done) => { @@ -21,7 +23,10 @@ export const passportLoader = async (app: Express) => { }) // sign in method with passport local strategy - passport.use(new LocalStrategy(async (email, password, done) => { + passport.use(new LocalStrategy({ + usernameField: 'email', + passwordField: 'password' + }, async (email, password, done) => { try { const response = await AuthInstance.login({ email, password }); return done(null, response); diff --git a/server/models/user.ts b/server/models/user.ts index c834e9e..f609695 100644 --- a/server/models/user.ts +++ b/server/models/user.ts @@ -50,13 +50,15 @@ export class User { handle = $3, email = $4, password = $5, - active = $6 - WHERE id = $7 + active = $6, + datemodified = $7 + WHERE id = $8 RETURNING *; ` const values = [ data.firstname, data.lastname, data.handle, - data.email, data.password, data.active, id + data.email, data.password, data.active, + data.datemodified, id ] const result = await pool.query(statement, values); diff --git a/server/package-lock.json b/server/package-lock.json index a8b297c..65218d9 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -9,9 +9,11 @@ "version": "1.0.0", "license": "ISC", "dependencies": { + "@types/cookie-parser": "^1.4.3", "bcrypt": "^5.1.0", "body-parser": "^1.20.1", "connect-pg-simple": "^8.0.0", + "cookie-parser": "^1.4.6", "cors": "^2.8.5", "dotenv": "^16.0.3", "express": "^4.18.2", @@ -204,7 +206,6 @@ "version": "1.19.2", "resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.19.2.tgz", "integrity": "sha512-ALYone6pm6QmwZoAgeyNksccT9Q4AWZQ6PvfwR37GT6r6FWUPguq6sUmNGSMV2Wr761oQoBxwGGa6DR5o1DC9g==", - "dev": true, "dependencies": { "@types/connect": "*", "@types/node": "*" @@ -214,7 +215,6 @@ "version": "3.4.35", "resolved": "https://registry.npmjs.org/@types/connect/-/connect-3.4.35.tgz", "integrity": "sha512-cdeYyv4KWoEgpBISTxWvqYsVy444DOqehiF3fM3ne10AmJ62RSyNkUnxMJXHQWRQQX2eR94m5y1IZyDwBjV9FQ==", - "dev": true, "dependencies": { "@types/node": "*" } @@ -230,6 +230,14 @@ "@types/pg": "*" } }, + "node_modules/@types/cookie-parser": { + "version": "1.4.3", + "resolved": "https://registry.npmjs.org/@types/cookie-parser/-/cookie-parser-1.4.3.tgz", + "integrity": "sha512-CqSKwFwefj4PzZ5n/iwad/bow2hTCh0FlNAeWLtQM3JA/NX/iYagIpWG2cf1bQKQ2c9gU2log5VUCrn7LDOs0w==", + "dependencies": { + "@types/express": "*" + } + }, "node_modules/@types/cors": { "version": "2.8.12", "resolved": "https://registry.npmjs.org/@types/cors/-/cors-2.8.12.tgz", @@ -250,7 +258,6 @@ "version": "4.17.14", "resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.14.tgz", "integrity": "sha512-TEbt+vaPFQ+xpxFLFssxUDXj5cWCxZJjIcB7Yg0k0GMHGtgtQgpvx/MUQUeAkNbA9AAGrwkAsoeItdTgS7FMyg==", - "dev": true, "dependencies": { "@types/body-parser": "*", "@types/express-serve-static-core": "^4.17.18", @@ -262,7 +269,6 @@ "version": "4.17.31", "resolved": "https://registry.npmjs.org/@types/express-serve-static-core/-/express-serve-static-core-4.17.31.tgz", "integrity": "sha512-DxMhY+NAsTwMMFHBTtJFNp5qiHKJ7TeqOo23zVEM9alT1Ml27Q3xcTH0xwxn7Q0BbMcVEJOs/7aQtUWupUQN3Q==", - "dev": true, "dependencies": { "@types/node": "*", "@types/qs": "*", @@ -293,8 +299,7 @@ "node_modules/@types/mime": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/@types/mime/-/mime-3.0.1.tgz", - "integrity": "sha512-Y4XFY5VJAuw0FgAqPNd6NNoV44jbq9Bz2L7Rh/J6jLTiHBSBJa9fxqQIvkIld4GsoDOcCbvzOUAbLPsSKKg+uA==", - "dev": true + "integrity": "sha512-Y4XFY5VJAuw0FgAqPNd6NNoV44jbq9Bz2L7Rh/J6jLTiHBSBJa9fxqQIvkIld4GsoDOcCbvzOUAbLPsSKKg+uA==" }, "node_modules/@types/morgan": { "version": "1.9.3", @@ -363,20 +368,17 @@ "node_modules/@types/qs": { "version": "6.9.7", "resolved": "https://registry.npmjs.org/@types/qs/-/qs-6.9.7.tgz", - "integrity": "sha512-FGa1F62FT09qcrueBA6qYTrJPVDzah9a+493+o2PCXsesWHIn27G98TsSMs3WPNbZIEj4+VJf6saSFpvD+3Zsw==", - "dev": true + "integrity": "sha512-FGa1F62FT09qcrueBA6qYTrJPVDzah9a+493+o2PCXsesWHIn27G98TsSMs3WPNbZIEj4+VJf6saSFpvD+3Zsw==" }, "node_modules/@types/range-parser": { "version": "1.2.4", "resolved": "https://registry.npmjs.org/@types/range-parser/-/range-parser-1.2.4.tgz", - "integrity": "sha512-EEhsLsD6UsDM1yFhAvy0Cjr6VwmpMWqFBCb9w07wVugF7w9nfajxLuVmngTIpgS6svCnm6Vaw+MZhoDCKnOfsw==", - "dev": true + "integrity": "sha512-EEhsLsD6UsDM1yFhAvy0Cjr6VwmpMWqFBCb9w07wVugF7w9nfajxLuVmngTIpgS6svCnm6Vaw+MZhoDCKnOfsw==" }, "node_modules/@types/serve-static": { "version": "1.15.0", "resolved": "https://registry.npmjs.org/@types/serve-static/-/serve-static-1.15.0.tgz", "integrity": "sha512-z5xyF6uh8CbjAu9760KDKsH2FcDxZ2tFCsA4HIMWE6IkiYMXfVoa+4f9KX+FN0ZLsaMw1WNG2ETLA6N+/YA+cg==", - "dev": true, "dependencies": { "@types/mime": "*", "@types/node": "*" @@ -805,6 +807,26 @@ "node": ">= 0.6" } }, + "node_modules/cookie-parser": { + "version": "1.4.6", + "resolved": "https://registry.npmjs.org/cookie-parser/-/cookie-parser-1.4.6.tgz", + "integrity": "sha512-z3IzaNjdwUC2olLIB5/ITd0/setiaFMLYiZJle7xg5Fe9KWAceil7xszYfHHBtDFYLSgJduS2Ty0P1uJdPDJeA==", + "dependencies": { + "cookie": "0.4.1", + "cookie-signature": "1.0.6" + }, + "engines": { + "node": ">= 0.8.0" + } + }, + "node_modules/cookie-parser/node_modules/cookie": { + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.4.1.tgz", + "integrity": "sha512-ZwrFkGJxUR3EIoXtO+yVE69Eb7KlixbaeAWfBQB9vVsNn/o+Yw69gBWSSDK825hQNdN+wF8zELf3dFNl/kxkUA==", + "engines": { + "node": ">= 0.6" + } + }, "node_modules/cookie-signature": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", @@ -2807,7 +2829,6 @@ "version": "1.19.2", "resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.19.2.tgz", "integrity": "sha512-ALYone6pm6QmwZoAgeyNksccT9Q4AWZQ6PvfwR37GT6r6FWUPguq6sUmNGSMV2Wr761oQoBxwGGa6DR5o1DC9g==", - "dev": true, "requires": { "@types/connect": "*", "@types/node": "*" @@ -2817,7 +2838,6 @@ "version": "3.4.35", "resolved": "https://registry.npmjs.org/@types/connect/-/connect-3.4.35.tgz", "integrity": "sha512-cdeYyv4KWoEgpBISTxWvqYsVy444DOqehiF3fM3ne10AmJ62RSyNkUnxMJXHQWRQQX2eR94m5y1IZyDwBjV9FQ==", - "dev": true, "requires": { "@types/node": "*" } @@ -2833,6 +2853,14 @@ "@types/pg": "*" } }, + "@types/cookie-parser": { + "version": "1.4.3", + "resolved": "https://registry.npmjs.org/@types/cookie-parser/-/cookie-parser-1.4.3.tgz", + "integrity": "sha512-CqSKwFwefj4PzZ5n/iwad/bow2hTCh0FlNAeWLtQM3JA/NX/iYagIpWG2cf1bQKQ2c9gU2log5VUCrn7LDOs0w==", + "requires": { + "@types/express": "*" + } + }, "@types/cors": { "version": "2.8.12", "resolved": "https://registry.npmjs.org/@types/cors/-/cors-2.8.12.tgz", @@ -2852,7 +2880,6 @@ "version": "4.17.14", "resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.14.tgz", "integrity": "sha512-TEbt+vaPFQ+xpxFLFssxUDXj5cWCxZJjIcB7Yg0k0GMHGtgtQgpvx/MUQUeAkNbA9AAGrwkAsoeItdTgS7FMyg==", - "dev": true, "requires": { "@types/body-parser": "*", "@types/express-serve-static-core": "^4.17.18", @@ -2864,7 +2891,6 @@ "version": "4.17.31", "resolved": "https://registry.npmjs.org/@types/express-serve-static-core/-/express-serve-static-core-4.17.31.tgz", "integrity": "sha512-DxMhY+NAsTwMMFHBTtJFNp5qiHKJ7TeqOo23zVEM9alT1Ml27Q3xcTH0xwxn7Q0BbMcVEJOs/7aQtUWupUQN3Q==", - "dev": true, "requires": { "@types/node": "*", "@types/qs": "*", @@ -2895,8 +2921,7 @@ "@types/mime": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/@types/mime/-/mime-3.0.1.tgz", - "integrity": "sha512-Y4XFY5VJAuw0FgAqPNd6NNoV44jbq9Bz2L7Rh/J6jLTiHBSBJa9fxqQIvkIld4GsoDOcCbvzOUAbLPsSKKg+uA==", - "dev": true + "integrity": "sha512-Y4XFY5VJAuw0FgAqPNd6NNoV44jbq9Bz2L7Rh/J6jLTiHBSBJa9fxqQIvkIld4GsoDOcCbvzOUAbLPsSKKg+uA==" }, "@types/morgan": { "version": "1.9.3", @@ -2964,20 +2989,17 @@ "@types/qs": { "version": "6.9.7", "resolved": "https://registry.npmjs.org/@types/qs/-/qs-6.9.7.tgz", - "integrity": "sha512-FGa1F62FT09qcrueBA6qYTrJPVDzah9a+493+o2PCXsesWHIn27G98TsSMs3WPNbZIEj4+VJf6saSFpvD+3Zsw==", - "dev": true + "integrity": "sha512-FGa1F62FT09qcrueBA6qYTrJPVDzah9a+493+o2PCXsesWHIn27G98TsSMs3WPNbZIEj4+VJf6saSFpvD+3Zsw==" }, "@types/range-parser": { "version": "1.2.4", "resolved": "https://registry.npmjs.org/@types/range-parser/-/range-parser-1.2.4.tgz", - "integrity": "sha512-EEhsLsD6UsDM1yFhAvy0Cjr6VwmpMWqFBCb9w07wVugF7w9nfajxLuVmngTIpgS6svCnm6Vaw+MZhoDCKnOfsw==", - "dev": true + "integrity": "sha512-EEhsLsD6UsDM1yFhAvy0Cjr6VwmpMWqFBCb9w07wVugF7w9nfajxLuVmngTIpgS6svCnm6Vaw+MZhoDCKnOfsw==" }, "@types/serve-static": { "version": "1.15.0", "resolved": "https://registry.npmjs.org/@types/serve-static/-/serve-static-1.15.0.tgz", "integrity": "sha512-z5xyF6uh8CbjAu9760KDKsH2FcDxZ2tFCsA4HIMWE6IkiYMXfVoa+4f9KX+FN0ZLsaMw1WNG2ETLA6N+/YA+cg==", - "dev": true, "requires": { "@types/mime": "*", "@types/node": "*" @@ -3311,6 +3333,22 @@ "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.5.0.tgz", "integrity": "sha512-YZ3GUyn/o8gfKJlnlX7g7xq4gyO6OSuhGPKaaGssGB2qgDUS0gPgtTvoyZLTt9Ab6dC4hfc9dV5arkvc/OCmrw==" }, + "cookie-parser": { + "version": "1.4.6", + "resolved": "https://registry.npmjs.org/cookie-parser/-/cookie-parser-1.4.6.tgz", + "integrity": "sha512-z3IzaNjdwUC2olLIB5/ITd0/setiaFMLYiZJle7xg5Fe9KWAceil7xszYfHHBtDFYLSgJduS2Ty0P1uJdPDJeA==", + "requires": { + "cookie": "0.4.1", + "cookie-signature": "1.0.6" + }, + "dependencies": { + "cookie": { + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.4.1.tgz", + "integrity": "sha512-ZwrFkGJxUR3EIoXtO+yVE69Eb7KlixbaeAWfBQB9vVsNn/o+Yw69gBWSSDK825hQNdN+wF8zELf3dFNl/kxkUA==" + } + } + }, "cookie-signature": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", diff --git a/server/package.json b/server/package.json index f5d4314..04acb4f 100644 --- a/server/package.json +++ b/server/package.json @@ -14,9 +14,11 @@ "author": "", "license": "ISC", "dependencies": { + "@types/cookie-parser": "^1.4.3", "bcrypt": "^5.1.0", "body-parser": "^1.20.1", "connect-pg-simple": "^8.0.0", + "cookie-parser": "^1.4.6", "cors": "^2.8.5", "dotenv": "^16.0.3", "express": "^4.18.2", diff --git a/server/routes/auth.ts b/server/routes/auth.ts index 056989a..1b09bad 100644 --- a/server/routes/auth.ts +++ b/server/routes/auth.ts @@ -2,24 +2,52 @@ import { Express, Router } from "express" import { PassportStatic } from "passport"; import { IUser, IUserAuth } from "../schemas"; import AuthService from "../auth"; +import { UserCtl } from "../controllers"; +import now from "../util/now"; const AuthInstance = new AuthService(); +const UserControl = new UserCtl(); const router = Router(); export const authRoute = (app: Express, passport: PassportStatic) => { app.use('/auth', router); + router.get('/', (req, res) => { + const data = { + session: req.session, + user: req.user + } + res.send(JSON.stringify(data)); + }) + router.post('/login', passport.authenticate('local'), async (req, res, next) => { try { const data: IUserAuth = req.body; - console.log(data); const response = await AuthInstance.login(data); - res.status(200).send(response); + console.log(response); + + if (response.ok) { + req.user = response.user; + await UserControl.updateOne(response.user.id, { ...response.user, datemodified: now }) + res.cookie('userid', response.user.id, { maxAge: 1000 * 60 * 60 * 24 * 7 }); + res.status(200).send(response.user); + } else { + res.status(401).send({ message: "Login unsuccessful" }); + } } catch(e) { next(e); } }) + router.post('/logout', passport.authenticate('local', async (req, res, next) => { + try { + if (req.session) req.session.destroy(); + res.sendStatus(200); + } catch(e) { + next(e); + } + })); + router.post('/register', async (req, res, next) => { try { const data: IUser = req.body; diff --git a/server/routes/index.ts b/server/routes/index.ts index c98434a..f10fdc8 100644 --- a/server/routes/index.ts +++ b/server/routes/index.ts @@ -16,8 +16,4 @@ export const routes = async (app: Express, passport: PassportStatic) => { collectionRoute(app); ingredientRoute(app); groceryListRoute(app); - - app.get('/hello', (req, res) => { - res.send({ message: "hello from the server!!" }); - }) } \ No newline at end of file