troubleshooting login route; passport local strategy
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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))({
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
})
|
||||
// ));
|
||||
}
|
||||
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user