troubleshooting login route; passport local strategy

This commit is contained in:
Mikayla Dobson
2022-07-17 11:32:51 -05:00
parent 31446f3cd0
commit af3ea629d6
7 changed files with 56 additions and 43 deletions

View File

@@ -23,8 +23,9 @@ function LoginForm() {
try { try {
const response = await handleLogin(username, password); const response = await handleLogin(username, password);
const json = await response?.json(); const json = await response?.json();
if (json) { if (json) {
console.log(json);
const { session, userProfile } = json; const { session, userProfile } = json;
let thisUser: userInfo = { let thisUser: userInfo = {
firstName: userProfile.first_name, firstName: userProfile.first_name,

View File

@@ -1,22 +1,22 @@
import { userInfo } from '../types/main'; import { userInfo } from '../types/main';
const APISTRING = 'http://localhost:8088/api'; const APISTRING = 'http://localhost:8088/api/';
export const getAllUsers = async () => { export const getAllUsers = async () => {
let serverCall = await fetch(APISTRING + '/users') let serverCall = await fetch(APISTRING + 'users')
.then(res => res.json()); .then(res => res.json());
return serverCall; return serverCall;
} }
export const getOneUser = async (email: string) => { 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()); .then(res => res.json());
return serverCall; return serverCall;
} }
export const registerNewUser = async (user: userInfo) => { export const registerNewUser = async (user: userInfo) => {
let serverCall = await fetch(APISTRING + '/register', { let serverCall = await fetch(APISTRING + 'register', {
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/json" "Content-Type": "application/json"
@@ -29,7 +29,9 @@ export const registerNewUser = async (user: userInfo) => {
} }
export const handleLogin = async (email: string, password: string) => { 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", method: "POST",
headers: { headers: {
"Content-Type": "application/json" "Content-Type": "application/json"
@@ -37,7 +39,7 @@ export const handleLogin = async (email: string, password: string) => {
body: JSON.stringify({ email: email, password: password }) body: JSON.stringify({ email: email, password: password })
}); });
return serverCall; return res;
} }
export const unwrapLogin = async (email: string, password: string) => { export const unwrapLogin = async (email: string, password: string) => {
@@ -48,7 +50,7 @@ export const unwrapLogin = async (email: string, password: string) => {
} }
export const getAllProducts = async () => { export const getAllProducts = async () => {
let serverCall = await fetch(APISTRING + '/products', { let serverCall = await fetch(APISTRING + 'products', {
method: "GET", method: "GET",
headers: { headers: {
"Content-Type": "application/json" "Content-Type": "application/json"
@@ -59,7 +61,7 @@ export const getAllProducts = async () => {
} }
export const getProductDetails = async (productID: string) => { export const getProductDetails = async (productID: string) => {
let serverCall = await fetch(`${APISTRING}/products/${productID}`, { let serverCall = await fetch(`${APISTRING}products/${productID}`, {
method: "GET", method: "GET",
headers: { headers: {
"Content-Type": "application/json" "Content-Type": "application/json"

View File

@@ -10,11 +10,14 @@ module.exports = (app) => {
app.use(bodyParser.json()); app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.urlencoded({ extended: true }));
app.set('trust proxy', 1);`` app.set('trust proxy', 1);
app.use(session({ app.use(session({
secret: process.env.EXPRESS_SECRET, secret: process.env.EXPRESS_SECRET,
cookie: { maxAge: 8 * 60 * 60 * 1000, secure: false }, cookie: {
maxAge: 8 * 60 * 60 * 1000,
secure: false
},
resave: false, resave: false,
saveUninitialized: false, saveUninitialized: false,
store: new (require('connect-pg-simple')(session))({ store: new (require('connect-pg-simple')(session))({

View File

@@ -4,8 +4,8 @@ const routes = require('../routes/API');
module.exports = async (app) => { module.exports = async (app) => {
const express = await expressLoader(app); const express = await expressLoader(app);
await passportLoader(express); const passport = await passportLoader(express);
await routes(app); await routes(app, passport);
console.log('loaders called'); console.log('loaders called');
} }

View File

@@ -1,5 +1,5 @@
const passport = require('passport'); const passport = require('passport');
const LocalStrategy = require('passport-local'); const LocalStrategy = require('passport-local').Strategy;
const { LoginService } = require('../services/Auth'); const { LoginService } = require('../services/Auth');
module.exports = (app) => { module.exports = (app) => {
@@ -19,10 +19,12 @@ module.exports = (app) => {
}) })
}); });
passport.use(new LocalStrategy({ passport.use(new LocalStrategy(
usernameField: 'email', {
passwordField: 'password' usernameField: 'email',
}, async (email, password, done) => { passwordField: 'password'
},
async (email, password, done) => {
try { try {
const response = await LoginService({ email: email, password: password }); const response = await LoginService({ email: email, password: password });
return done(null, response); return done(null, response);

View File

@@ -2,7 +2,32 @@ const loginRouter = require('express').Router();
const { LoginService } = require('../services/Auth'); const { LoginService } = require('../services/Auth');
module.exports = (app, passport) => { 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) => // loginRouter.post('/', (req, res) =>
// passport.authenticate('local', { successRedirect: '/', failureRedirect: '/login' }) // passport.authenticate('local', { successRedirect: '/', failureRedirect: '/login' })
@@ -15,28 +40,5 @@ module.exports = (app, passport) => {
// next(e); // 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);
}
})
} }

View File

@@ -4,6 +4,7 @@ const bcrypt = require('bcrypt');
async function LoginService(email, password) { async function LoginService(email, password) {
const client = await connect(); const client = await connect();
let session; let session;
console.log('login service');
try { try {
let hash = await client.query("SELECT password FROM users WHERE email = ($1)", [email]); 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]); let fullUserProfile = await client.query("SELECT * FROM users WHERE email = ($1)", [email]);
console.log({session, userProfile: fullUserProfile.rows[0]});
return { return {
session: session, session: session,
userProfile: fullUserProfile.rows[0] userProfile: fullUserProfile.rows[0]