From 7103019890960e793deefb64987a09b33be60b42 Mon Sep 17 00:00:00 2001 From: Michael Hunteman Date: Fri, 17 May 2024 15:20:30 -0700 Subject: Add golang server --- client/src/features/auth/GuestLogin.tsx | 79 +++++++++++++++++++++++++++++++++ client/src/features/auth/authSlice.ts | 26 +++++++++++ 2 files changed, 105 insertions(+) create mode 100644 client/src/features/auth/GuestLogin.tsx create mode 100644 client/src/features/auth/authSlice.ts (limited to 'client/src/features/auth') diff --git a/client/src/features/auth/GuestLogin.tsx b/client/src/features/auth/GuestLogin.tsx new file mode 100644 index 0000000..4da7e45 --- /dev/null +++ b/client/src/features/auth/GuestLogin.tsx @@ -0,0 +1,79 @@ +import React from 'react'; +import { useNavigate } from 'react-router-dom'; +import { useDispatch } from 'react-redux'; +import { Button, Container, TextField, Typography } from '@mui/material'; +import { useForm } from 'react-hook-form'; +import { setCredentials } from './authSlice'; +import { useLoginMutation } from '../../apiSlice'; +import type { LoginRequest } from '../../apiSlice'; + +function GuestLogin() { + const dispatch = useDispatch(); + const navigate = useNavigate(); + const [login] = useLoginMutation(); + + const { + register, + handleSubmit, + formState: { errors }, + } = useForm({ + defaultValues: { + firstName: '', + lastName: '', + }, + }); + + const onSubmit = async (data: LoginRequest) => { + try { + dispatch(setCredentials(await login(data).unwrap())); + navigate('/rsvp'); + } catch (e) { + console.log(e); + } + }; + + return ( + +
+ Guest Login + + + +
+
+ ); +} + +export default GuestLogin; diff --git a/client/src/features/auth/authSlice.ts b/client/src/features/auth/authSlice.ts new file mode 100644 index 0000000..bff2bdd --- /dev/null +++ b/client/src/features/auth/authSlice.ts @@ -0,0 +1,26 @@ +import { createSlice } from '@reduxjs/toolkit'; +import type { RootState } from '../../store'; +import type { Guest } from '../../apiSlice'; + +type AuthState = { + guest?: Guest; + token?: string; +}; + +const authSlice = createSlice({ + name: 'auth', + initialState: { guest: undefined, token: undefined } as AuthState, + reducers: { + setCredentials: (state, action) => { + const { guest, token } = action.payload; + state.guest = guest; + state.token = token; + }, + }, +}); + +export const { setCredentials } = authSlice.actions; + +export default authSlice.reducer; + +export const selectCurrentGuest = (state: RootState) => state.auth.guest; -- cgit v1.2.3