defined auth route on api, attempting to connect to front end
This commit is contained in:
@@ -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 }));
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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());
|
||||
|
||||
|
||||
@@ -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
31
server/routes/auth.ts
Normal 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);
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user