diff --git a/client/src/components/pages/Login.tsx b/client/src/components/pages/Login.tsx index 01567d7..76ebf01 100644 --- a/client/src/components/pages/Login.tsx +++ b/client/src/components/pages/Login.tsx @@ -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() { + const [input, setInput] = useState({ + email: '', + password: '' + }) + + const handleLogin = async () => { + if (!input.email || !input.password) return; + } + return (

Hello! Nice to see you again.

- {/* login form */} + + + setInput({...input, email: e.target.value})}> + + setInput({...input, password: e.target.value})}> + + - -

Not registered yet? You can do that here.

+
) } \ No newline at end of file diff --git a/client/src/schemas/index.ts b/client/src/schemas/index.ts new file mode 120000 index 0000000..47c0708 --- /dev/null +++ b/client/src/schemas/index.ts @@ -0,0 +1 @@ +/Users/mikayladobson/Developer/PORTFOLIO/recipe-manager/server/schemas/index.ts \ No newline at end of file diff --git a/client/src/util/apiUtils.ts b/client/src/util/apiUtils.ts index fca05d3..7408c72 100644 --- a/client/src/util/apiUtils.ts +++ b/client/src/util/apiUtils.ts @@ -1,5 +1,21 @@ +import { IUser } from "../schemas"; 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 export const getRecipeByID = async (id: string | number) => { const result = await fetch(API + 'recipe/' + id).then(response => response.json()); diff --git a/client/src/util/types.ts b/client/src/util/types.ts index e2f7c86..705b086 100644 --- a/client/src/util/types.ts +++ b/client/src/util/types.ts @@ -1,4 +1,5 @@ import { FC, ReactNode } from "react"; +import * as schemas from "../schemas"; interface PortalBase { children?: ReactNode @@ -11,45 +12,4 @@ interface ButtonParams extends PortalBase { export type PageComponent = FC export type PanelComponent = FC -export type ButtonComponent = FC - -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"] -} \ No newline at end of file +export type ButtonComponent = FC \ No newline at end of file diff --git a/server/loaders/express.ts b/server/loaders/express.ts index 157c6c0..b553c0a 100644 --- a/server/loaders/express.ts +++ b/server/loaders/express.ts @@ -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 })); diff --git a/server/loaders/index.ts b/server/loaders/index.ts index bf91ac7..396365d 100644 --- a/server/loaders/index.ts +++ b/server/loaders/index.ts @@ -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); } \ No newline at end of file diff --git a/server/loaders/passport.ts b/server/loaders/passport.ts index 43df1e7..cbec2f9 100644 --- a/server/loaders/passport.ts +++ b/server/loaders/passport.ts @@ -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()); diff --git a/server/loaders/swagger.ts b/server/loaders/swagger.ts index f6d2f7d..0535b73 100644 --- a/server/loaders/swagger.ts +++ b/server/loaders/swagger.ts @@ -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!)); } \ No newline at end of file diff --git a/server/routes/auth.ts b/server/routes/auth.ts new file mode 100644 index 0000000..2dfc6ac --- /dev/null +++ b/server/routes/auth.ts @@ -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); + } + }) +} \ No newline at end of file diff --git a/server/routes/index.ts b/server/routes/index.ts index 7d8bb3f..c98434a 100644 --- a/server/routes/index.ts +++ b/server/routes/index.ts @@ -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);