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

@@ -1,14 +1,30 @@
import { Button, Page } from "../ui"; import { useState } from "react";
import { IUserAuth } from "../../schemas";
import { Button, Page, Panel } from "../ui";
export default function Login() { export default function Login() {
const [input, setInput] = useState<IUserAuth>({
email: '',
password: ''
})
const handleLogin = async () => {
if (!input.email || !input.password) return;
}
return ( return (
<Page> <Page>
<h1>Hello! Nice to see you again.</h1> <h1>Hello! Nice to see you again.</h1>
{/* login form */} <Panel extraStyles="form-panel">
<label htmlFor="login-email">Email</label>
<input type="text" id="login-email" onChange={(e) => setInput({...input, email: e.target.value})}></input>
<label htmlFor="login-password">Password</label>
<input type="text" id="login-password" onChange={(e) => setInput({...input, password: e.target.value})}></input>
<Button onClick={() => {}}>Log In</Button>
</Panel>
<Button onClick={() => {}}>Log In</Button> <aside>Not registered yet? You can do that <a href="/register">here.</a></aside>
<p>Not registered yet? You can do that here.</p>
</Page> </Page>
) )
} }

1
client/src/schemas/index.ts Symbolic link
View File

@@ -0,0 +1 @@
/Users/mikayladobson/Developer/PORTFOLIO/recipe-manager/server/schemas/index.ts

View File

@@ -1,5 +1,21 @@
import { IUser } from "../schemas";
const API = import.meta.env.APISTRING || "http://localhost:8080/"; const API = import.meta.env.APISTRING || "http://localhost:8080/";
// auth handlers
export const attemptLogin = async (email: string, password: string) => {
const result = await fetch(API + 'auth/login/', { method: "POST" })
.then(response => response.json());
return result;
}
export const attemptRegister = async (data: IUser) => {
const result = await fetch(API + 'auth/register/', { method: "POST" })
.then(response => response.json());
return result;
}
// on recipe route // on recipe route
export const getRecipeByID = async (id: string | number) => { export const getRecipeByID = async (id: string | number) => {
const result = await fetch(API + 'recipe/' + id).then(response => response.json()); const result = await fetch(API + 'recipe/' + id).then(response => response.json());

View File

@@ -1,4 +1,5 @@
import { FC, ReactNode } from "react"; import { FC, ReactNode } from "react";
import * as schemas from "../schemas";
interface PortalBase { interface PortalBase {
children?: ReactNode children?: ReactNode
@@ -11,45 +12,4 @@ interface ButtonParams extends PortalBase {
export type PageComponent = FC<PortalBase> export type PageComponent = FC<PortalBase>
export type PanelComponent = FC<PortalBase> export type PanelComponent = FC<PortalBase>
export type ButtonComponent = FC<ButtonParams> export type ButtonComponent = FC<ButtonParams>
export interface IUser {
id: number
firstname: string
lastname: string
handle: string
email: string
password: string
active: boolean
}
export interface IRecipe {
id: number
name: string
description?: string
preptime: string
removed: boolean
authoruserid: IUser["id"]
}
export interface IIngredient {
id: number
name: string
description?: string
}
export interface ICollection {
id: number
name: string
active: string
ismaincollection: boolean
ownerid: IUser["id"]
}
export interface IGroceryList {
id: number
name: string
recipes?: IRecipe["id"][]
active: boolean
ownerid: IUser["id"]
}

View File

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

View File

@@ -2,11 +2,11 @@ import { Express } from 'express';
import { expressLoader } from './express'; import { expressLoader } from './express';
import { swaggerLoader } from './swagger'; import { swaggerLoader } from './swagger';
import { routes } from '../routes'; import { routes } from '../routes';
import { passportLoader } from './passport';
export const loaders = async (app: Express) => { export const loaders = async (app: Express) => {
const expressApp = await expressLoader(app); const expressApp = await expressLoader(app);
// const passportApp = await passportLoader(expressApp); const passportApp = await passportLoader(expressApp);
await swaggerLoader(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"; import { IUserAuth } from "../schemas";
const AuthInstance = new AuthService(); const AuthInstance = new AuthService();
export const passportApp = (app: Express) => { export const passportLoader = async (app: Express) => {
app.use(passport.initialize()); app.use(passport.initialize());
app.use(passport.session()); 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')); 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!)); 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 { Express } from "express"
import { PassportStatic } from "passport";
import { userRoute } from "./users"; import { userRoute } from "./users";
import { recipeRoute } from "./recipe"; import { recipeRoute } from "./recipe";
import { collectionRoute } from "./collection"; import { collectionRoute } from "./collection";
import { ingredientRoute } from "./ingredient"; import { ingredientRoute } from "./ingredient";
import { groceryListRoute } from "./groceryList"; 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'); console.log('routes called');
authRoute(app, passport);
userRoute(app); userRoute(app);
recipeRoute(app); recipeRoute(app);
collectionRoute(app); collectionRoute(app);