diff --git a/client/src/components/User/LoginForm.tsx b/client/src/components/User/LoginForm.tsx index 915a105..b00be93 100644 --- a/client/src/components/User/LoginForm.tsx +++ b/client/src/components/User/LoginForm.tsx @@ -23,8 +23,9 @@ function LoginForm() { try { const response = await handleLogin(username, password); const json = await response?.json(); - + if (json) { + console.log(json); const { session, userProfile } = json; let thisUser: userInfo = { firstName: userProfile.first_name, diff --git a/client/src/util/apiUtils.ts b/client/src/util/apiUtils.ts index 0a9f18c..36e3850 100644 --- a/client/src/util/apiUtils.ts +++ b/client/src/util/apiUtils.ts @@ -1,22 +1,22 @@ import { userInfo } from '../types/main'; -const APISTRING = 'http://localhost:8088/api'; +const APISTRING = 'http://localhost:8088/api/'; export const getAllUsers = async () => { - let serverCall = await fetch(APISTRING + '/users') + let serverCall = await fetch(APISTRING + 'users') .then(res => res.json()); return serverCall; } export const getOneUser = async (email: string) => { - let serverCall = await fetch(`${APISTRING}/users?email=${email}`) + let serverCall = await fetch(`${APISTRING}users?email=${email}`) .then(res => res.json()); return serverCall; } export const registerNewUser = async (user: userInfo) => { - let serverCall = await fetch(APISTRING + '/register', { + let serverCall = await fetch(APISTRING + 'register', { method: "POST", headers: { "Content-Type": "application/json" @@ -29,7 +29,9 @@ export const registerNewUser = async (user: userInfo) => { } export const handleLogin = async (email: string, password: string) => { - let serverCall = await fetch(APISTRING + '/login', { + const url = APISTRING + 'login'; + console.log(url); + const res = await fetch(url, { method: "POST", headers: { "Content-Type": "application/json" @@ -37,7 +39,7 @@ export const handleLogin = async (email: string, password: string) => { body: JSON.stringify({ email: email, password: password }) }); - return serverCall; + return res; } export const unwrapLogin = async (email: string, password: string) => { @@ -48,7 +50,7 @@ export const unwrapLogin = async (email: string, password: string) => { } export const getAllProducts = async () => { - let serverCall = await fetch(APISTRING + '/products', { + let serverCall = await fetch(APISTRING + 'products', { method: "GET", headers: { "Content-Type": "application/json" @@ -59,7 +61,7 @@ export const getAllProducts = async () => { } export const getProductDetails = async (productID: string) => { - let serverCall = await fetch(`${APISTRING}/products/${productID}`, { + let serverCall = await fetch(`${APISTRING}products/${productID}`, { method: "GET", headers: { "Content-Type": "application/json" diff --git a/loaders/express.js b/loaders/express.js index 8341881..2ae9964 100644 --- a/loaders/express.js +++ b/loaders/express.js @@ -10,11 +10,14 @@ module.exports = (app) => { app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); - app.set('trust proxy', 1);`` + app.set('trust proxy', 1); app.use(session({ secret: process.env.EXPRESS_SECRET, - cookie: { maxAge: 8 * 60 * 60 * 1000, secure: false }, + cookie: { + maxAge: 8 * 60 * 60 * 1000, + secure: false + }, resave: false, saveUninitialized: false, store: new (require('connect-pg-simple')(session))({ diff --git a/loaders/index.js b/loaders/index.js index 7f5ef10..7ef64c2 100644 --- a/loaders/index.js +++ b/loaders/index.js @@ -4,8 +4,8 @@ const routes = require('../routes/API'); module.exports = async (app) => { const express = await expressLoader(app); - await passportLoader(express); - await routes(app); + const passport = await passportLoader(express); + await routes(app, passport); console.log('loaders called'); } \ No newline at end of file diff --git a/loaders/passport.js b/loaders/passport.js index 90e731d..148aa9c 100644 --- a/loaders/passport.js +++ b/loaders/passport.js @@ -1,5 +1,5 @@ const passport = require('passport'); -const LocalStrategy = require('passport-local'); +const LocalStrategy = require('passport-local').Strategy; const { LoginService } = require('../services/Auth'); module.exports = (app) => { @@ -19,10 +19,12 @@ module.exports = (app) => { }) }); - passport.use(new LocalStrategy({ - usernameField: 'email', - passwordField: 'password' - }, async (email, password, done) => { + passport.use(new LocalStrategy( + { + usernameField: 'email', + passwordField: 'password' + }, + async (email, password, done) => { try { const response = await LoginService({ email: email, password: password }); return done(null, response); diff --git a/routes/login.js b/routes/login.js index edeb758..fb44319 100644 --- a/routes/login.js +++ b/routes/login.js @@ -2,7 +2,32 @@ const loginRouter = require('express').Router(); const { LoginService } = require('../services/Auth'); module.exports = (app, passport) => { - app.use('/api/login', loginRouter); + app.use( + loginRouter.post('/api/login', passport.authenticate("local"), async (req, res, next) => { + const { email, password } = req.body; + + /** + * @function LoginService + * @returns: object, with keys: + * session: session object + * userProfile: postgres response from query + * + * session object: + * authenticated: boolean, + * user: { email, password } + */ + + try { + const data = await LoginService(email, password); + const { session, userProfile } = data; + + req.session.id = session.id; + res.status(200).send({ session, userProfile }); + } catch(e) { + next(e); + } + }) + ); // loginRouter.post('/', (req, res) => // passport.authenticate('local', { successRedirect: '/', failureRedirect: '/login' }) @@ -15,28 +40,5 @@ module.exports = (app, passport) => { // next(e); // } // } - // )); - - loginRouter.post('/', async (req, res, next) => { - const { email, password } = req.body; - - /** - * @function LoginService - * @returns: object, with keys: - * session: session object - * userProfile: postgres response from query - * - * session object: - * authenticated: boolean, - * user: { email, password } - */ - - try { - const data = await LoginService(email, password); - const { session, userProfile } = data; - res.status(200).send({ session, userProfile }); - } catch(e) { - next(e); - } - }) + // )); } \ No newline at end of file diff --git a/services/Auth.js b/services/Auth.js index a98480d..1a21433 100644 --- a/services/Auth.js +++ b/services/Auth.js @@ -4,6 +4,7 @@ const bcrypt = require('bcrypt'); async function LoginService(email, password) { const client = await connect(); let session; + console.log('login service'); try { let hash = await client.query("SELECT password FROM users WHERE email = ($1)", [email]); @@ -25,6 +26,8 @@ async function LoginService(email, password) { let fullUserProfile = await client.query("SELECT * FROM users WHERE email = ($1)", [email]); + console.log({session, userProfile: fullUserProfile.rows[0]}); + return { session: session, userProfile: fullUserProfile.rows[0]