everything switched over, time to test

This commit is contained in:
Luke Berndt
2023-03-30 22:31:15 -04:00
parent 5875364502
commit b8e60ab926
14 changed files with 1214 additions and 1024 deletions

View File

@@ -22,11 +22,33 @@ export const authenticateUser = createAsyncThunk(
export const loginUser = createAsyncThunk(
'user/login',
async ({ getState }) => {
async (data) => {
const url = process.env.REACT_APP_ACCOUNT_SERVER + "/login"
const res = await fetch(url, {
method: 'POST',
credentials: "include"
credentials: "include",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data)
}).then(
(data) => data.json()
)
return res;
}
)
export const registerUser = createAsyncThunk(
'user/register',
async (data) => {
const url = process.env.REACT_APP_ACCOUNT_SERVER + "/register"
const res = await fetch(url, {
method: 'POST',
credentials: "include",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data)
}).then(
(data) => data.json()
)
@@ -36,6 +58,24 @@ export const loginUser = createAsyncThunk(
export const updateProfile = createAsyncThunk(
'user/updateProfile',
async (user) => {
const url = process.env.REACT_APP_ACCOUNT_SERVER + "/users/" + user.userId;
const res = await fetch(url, {
method: 'POST',
credentials: "include",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(user)
}).then(
(data) => data.json()
)
return res;
}
)
export const logoutUser = createAsyncThunk(
'user/logout',
async ({ getState }) => {
@@ -50,6 +90,63 @@ export const logoutUser = createAsyncThunk(
}
)
export const sendConfirmEmail = createAsyncThunk(
'user/sendConfirmEmail',
async (userId) => {
const url = process.env.REACT_APP_ACCOUNT_SERVER + "/users/" + userId + "/send-confirm";
const res = await fetch(url, {
method: 'POST',
credentials: "include",
headers: {
"Content-Type": "application/json",
}
}).then(
(data) => data.json()
)
return res;
}
)
export const resetPassword = createAsyncThunk(
'user/resetPassword',
async (data) => {
const {userId, token, password} = data;
const url = process.env.REACT_APP_ACCOUNT_SERVER + "/users/" + userId + "/confirm/" + token
const res = await fetch(url, {
method: 'POST',
credentials: "include",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({password})
}).then(
(data) => data.json()
)
return res;
}
)
export const sendResetPassword = createAsyncThunk(
'user/sendResetPassword',
async (data) => {
const {userId, token, password} = data;
const url = process.env.REACT_APP_ACCOUNT_SERVER + "/api/send-reset-password"
const res = await fetch(url, {
method: 'POST',
credentials: "include",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data)
}).then(
(data) => data.json()
)
return res;
}
)
export const userSlice = createSlice({
name: 'user',
initialState: {
@@ -59,7 +156,12 @@ export const userSlice = createSlice({
userId: "",
admin: false,
email: "",
firstName: "",
lastName: "",
location: "",
screenName: ""
},
reducers: {
increment: (state) => {
// Redux Toolkit allows us to write "mutating" logic in reducers. It
@@ -70,6 +172,44 @@ export const userSlice = createSlice({
},
},
extraReducers: {
[loginUser.fulfilled]: (state, data) => {
const {payload} = data;
if (payload.success) {
state.authenticated = true;
state.hasAuthenticated = true;
state.userId = payload.user.userId;
state.admin = payload.user.admin;
state.email = payload.user.email;
state.firstName = payload.user.firstName;
state.lastName = payload.user.lastName;
state.location = payload.user.location;
state.screenName = payload.user.screenName;
} else {
state.authenticated = false;
state.hasAuthenticated = true;
}
},
[updateProfile.fulfilled]: (state, data) => {
const {payload} = data;
if (payload.success) {
state.authenticated = true;
state.hasAuthenticated = true;
state.userId = payload.user.userId;
state.admin = payload.user.admin;
state.email = payload.user.email;
state.firstName = payload.user.firstName;
state.lastName = payload.user.lastName;
state.location = payload.user.location;
state.screenName = payload.user.screenName;
} else {
state.authenticated = false;
state.hasAuthenticated = true;
}
},
[loginUser.rejected]: (state, { payload }) => {
state.authenticated = false;
state.hasAuthenticated = true;
},
[authenticateUser.fulfilled]: (state, data) => {
const {payload} = data;
if (payload.success) {
@@ -78,6 +218,10 @@ export const userSlice = createSlice({
state.userId = payload.user.userId;
state.admin = payload.user.admin;
state.email = payload.user.email;
state.firstName = payload.user.firstName;
state.lastName = payload.user.lastName;
state.location = payload.user.location;
state.screenName = payload.user.screenName;
} else {
state.authenticated = false;
state.hasAuthenticated = true;