Skip to content

Commit

Permalink
Create auth.actions.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Aug 9, 2024
1 parent e978a11 commit 949aade
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions projects/pi-nexus-iam/actions/auth.actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { createAction, createReducer } from '@reduxjs/toolkit';
import { AuthState } from '../models/auth';
import { login, logout, refreshToken } from '../services/auth.service';

export const loginAction = createAction('auth/login', (email: string, password: string) => {
return async (dispatch: any) => {
try {
const response = await login(email, password);
dispatch(setAuthState(response.data));
} catch (error) {
console.error(error);
}
};
});

export const logoutAction = createAction('auth/logout', () => {
return async (dispatch: any) => {
try {
await logout();
dispatch(setAuthState(null));
} catch (error) {
console.error(error);
}
};
});

export const refreshTokenAction = createAction('auth/refreshToken', () => {
return async (dispatch: any) => {
try {
const response = await refreshToken();
dispatch(setAuthState(response.data));
} catch (error) {
console.error(error);
}
};
});

const setAuthState = createAction('auth/setAuthState', (authState: AuthState) => {
return { authState };
});

const initialState: AuthState = null;

const authReducer = createReducer(initialState, {
[setAuthState]: (state, action) => action.authState,
});

export default authReducer;

0 comments on commit 949aade

Please sign in to comment.