defined auth route on api, attempting to connect to front end

This commit is contained in:
Mikayla Dobson
2022-11-21 11:22:00 -06:00
parent 1b08598799
commit 57a16e429e
10 changed files with 80 additions and 53 deletions

View File

@@ -5,7 +5,7 @@ import cors from 'cors';
import session from 'express-session';
import pgSessionStore from '../db/sessionStore';
export const expressLoader = (app: Express) => {
export const expressLoader = async (app: Express) => {
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

View File

@@ -2,11 +2,11 @@ import { Express } from 'express';
import { expressLoader } from './express';
import { swaggerLoader } from './swagger';
import { routes } from '../routes';
import { passportLoader } from './passport';
export const loaders = async (app: Express) => {
const expressApp = await expressLoader(app);
// const passportApp = await passportLoader(expressApp);
const passportApp = await passportLoader(expressApp);
await swaggerLoader(expressApp);
await routes(expressApp);
await routes(expressApp, passportApp);
}

View File

@@ -5,7 +5,7 @@ import AuthService from "../auth";
import { IUserAuth } from "../schemas";
const AuthInstance = new AuthService();
export const passportApp = (app: Express) => {
export const passportLoader = async (app: Express) => {
app.use(passport.initialize());
app.use(passport.session());

View File

@@ -6,6 +6,6 @@ import path from 'path';
const swaggerDocument = yaml.load(fs.readFileSync(path.resolve(__dirname, '../swagger.yaml'), 'utf-8'));
export const swaggerLoader = (app: Express) => {
export const swaggerLoader = async (app: Express) => {
app.use('/api-docs', swaggerUI.serve, swaggerUI.setup(swaggerDocument!));
}

31
server/routes/auth.ts Normal file
View File

@@ -0,0 +1,31 @@
import { Express, Router } from "express"
import { PassportStatic } from "passport";
import { IUser, IUserAuth } from "../schemas";
import AuthService from "../auth";
const AuthInstance = new AuthService();
const router = Router();
export const authRoute = (app: Express, passport: PassportStatic) => {
app.use('/auth', router);
router.post('/login', passport.authenticate('local'), async (req, res, next) => {
try {
const data: IUserAuth = req.body;
const response = await AuthInstance.login(data);
res.status(200).send(response);
} catch(e) {
next(e);
}
})
router.post('/register', async (req, res, next) => {
try {
const data: IUser = req.body;
const response = await AuthInstance.register(data);
res.status(200).send(response);
} catch(e) {
next(e);
}
})
}

View File

@@ -1,13 +1,16 @@
import { Express } from "express"
import { PassportStatic } from "passport";
import { userRoute } from "./users";
import { recipeRoute } from "./recipe";
import { collectionRoute } from "./collection";
import { ingredientRoute } from "./ingredient";
import { groceryListRoute } from "./groceryList";
import { authRoute } from "./auth";
export const routes = (app: Express, passport?: any) => {
export const routes = async (app: Express, passport: PassportStatic) => {
console.log('routes called');
authRoute(app, passport);
userRoute(app);
recipeRoute(app);
collectionRoute(app);