73 lines
1.5 KiB
TypeScript
73 lines
1.5 KiB
TypeScript
/*
|
|
* File: authSlice.ts
|
|
* Description: Redux slice for Auth state management
|
|
* Design & Developed by Tech4Biz Solutions
|
|
* Copyright (c) Spurrin Innovations. All rights reserved.
|
|
*/
|
|
|
|
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
|
|
|
|
// Sample user type
|
|
export interface User {
|
|
id: string;
|
|
name: string;
|
|
email: string;
|
|
}
|
|
|
|
// Auth state type
|
|
interface AuthState {
|
|
user: User | null;
|
|
loading: boolean;
|
|
error: string | null;
|
|
isAuthenticated: boolean;
|
|
}
|
|
|
|
const initialState: AuthState = {
|
|
user: null,
|
|
loading: false,
|
|
error: null,
|
|
isAuthenticated: false,
|
|
};
|
|
|
|
/**
|
|
* Auth slice for managing authentication state
|
|
*/
|
|
const authSlice = createSlice({
|
|
name: 'auth',
|
|
initialState,
|
|
reducers: {
|
|
loginStart(state) {
|
|
state.loading = true;
|
|
state.error = null;
|
|
},
|
|
loginSuccess(state, action: PayloadAction<User>) {
|
|
state.user = action.payload;
|
|
state.isAuthenticated = true;
|
|
state.loading = false;
|
|
},
|
|
loginFailure(state, action: PayloadAction<string>) {
|
|
state.loading = false;
|
|
state.error = action.payload;
|
|
state.isAuthenticated = false;
|
|
},
|
|
logout(state) {
|
|
state.user = null;
|
|
state.isAuthenticated = false;
|
|
},
|
|
},
|
|
});
|
|
|
|
export const {
|
|
loginStart,
|
|
loginSuccess,
|
|
loginFailure,
|
|
logout,
|
|
} = authSlice.actions;
|
|
|
|
export default authSlice.reducer;
|
|
|
|
/*
|
|
* End of File: authSlice.ts
|
|
* Design & Developed by Tech4Biz Solutions
|
|
* Copyright (c) Spurrin Innovations. All rights reserved.
|
|
*/
|