update, consume for login done
This commit is contained in:
@ -17,37 +17,7 @@ import { doSaveLogActivity } from '@/actions/GlobalActions';
|
||||
const API_URL = apiConfig.service_dashboard;
|
||||
|
||||
export const LOGIN_URL = `${API_URL}/login`;
|
||||
export const FORGOT_PASSWORD_URL = `${API_URL}/reset_password`;
|
||||
export const RESET_PASSWORD_URL = `${API_URL}/update_password`;
|
||||
export const GET_USER_URL = `${API_URL}/user/detail`;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// DUMMY MODE — set to `false` to restore the real API-backed auth flow.
|
||||
// While `true`, every method below skips its axios call and works off a
|
||||
// local, hardcoded session instead. This keeps the rest of the app (route
|
||||
// guards, `getAuth()`, etc.) working without a live backend.
|
||||
// ---------------------------------------------------------------------------
|
||||
const DUMMY_MODE = true;
|
||||
|
||||
const DUMMY_USER: UserModel = {
|
||||
id: 1,
|
||||
name: 'Administrator',
|
||||
username: 'admin',
|
||||
email: 'admin@example.com',
|
||||
password: undefined,
|
||||
customer: { id: 'dummy-customer-id' },
|
||||
role_name: 'Admin'
|
||||
};
|
||||
|
||||
const DUMMY_AUTH: AuthModel = {
|
||||
id: 'dummy-user-id',
|
||||
username: 'admin',
|
||||
role_name: 'Admin',
|
||||
user: DUMMY_USER,
|
||||
statusbalance: 'Y',
|
||||
access_token: 'dummy-token',
|
||||
token_type: 'Bearer'
|
||||
};
|
||||
export const GET_USER_URL = `${API_URL}/user/profile`;
|
||||
|
||||
interface AuthContextProps {
|
||||
loading: boolean;
|
||||
@ -57,9 +27,6 @@ interface AuthContextProps {
|
||||
currentUser: UserModel | undefined;
|
||||
setCurrentUser: Dispatch<SetStateAction<UserModel | undefined>>;
|
||||
login: (email: string, password: string, token?: string) => Promise<void>;
|
||||
requestPasswordResetLink: (email: string) => Promise<void>;
|
||||
changePassword: (token: string, password: string, password_confirmation: string) => Promise<void>;
|
||||
getUser: () => Promise<AxiosResponse<any> | {}>;
|
||||
logout: () => void;
|
||||
verify: () => Promise<void>;
|
||||
}
|
||||
@ -81,25 +48,16 @@ const AuthProvider = ({ children }: PropsWithChildren) => {
|
||||
};
|
||||
|
||||
const login = async (username: string, password: string, token?: string) => {
|
||||
if (DUMMY_MODE) {
|
||||
// No API call — just persist a local dummy session.
|
||||
saveAuth(DUMMY_AUTH);
|
||||
setCurrentUser(DUMMY_USER);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const { data: auth } = await axios
|
||||
.post(LOGIN_URL, { username, password, token })
|
||||
.post(LOGIN_URL, { username, password })
|
||||
.then((response) => response.data);
|
||||
|
||||
const enhancedAuth: AuthModel = {
|
||||
...auth.token,
|
||||
id: auth.user.id,
|
||||
role_name: auth.role_name,
|
||||
id: auth.userid,
|
||||
user: auth.user,
|
||||
statusbalance: auth.role?.status_balance ?? null // SAFE ACCESS
|
||||
};
|
||||
};
|
||||
|
||||
saveAuth(enhancedAuth);
|
||||
setCurrentUser(auth.user);
|
||||
@ -116,45 +74,15 @@ const AuthProvider = ({ children }: PropsWithChildren) => {
|
||||
}
|
||||
};
|
||||
|
||||
const requestPasswordResetLink = async (email: string) => {
|
||||
if (DUMMY_MODE) {
|
||||
console.warn('[AuthProvider] DUMMY_MODE: requestPasswordResetLink skipped (no API call).');
|
||||
return;
|
||||
}
|
||||
await axios.put(FORGOT_PASSWORD_URL + '/' + email);
|
||||
};
|
||||
|
||||
const changePassword = async (token: string, password: string, retype_password: string) => {
|
||||
if (DUMMY_MODE) {
|
||||
console.warn('[AuthProvider] DUMMY_MODE: changePassword skipped (no API call).');
|
||||
return;
|
||||
}
|
||||
await axios.put(`${RESET_PASSWORD_URL}/${token}`, {
|
||||
password,
|
||||
retype_password
|
||||
});
|
||||
};
|
||||
|
||||
const getUser = async () => {
|
||||
if (DUMMY_MODE) {
|
||||
return { data: DUMMY_USER };
|
||||
}
|
||||
|
||||
let _axios = await axios
|
||||
.get(`${GET_USER_URL}/${authHelper.getAuth()?.id}`)
|
||||
.get(`${GET_USER_URL}/${authHelper.getAuth()?.user?.id}`)
|
||||
.then((response) => response.data.data);
|
||||
|
||||
return { data: _axios };
|
||||
};
|
||||
|
||||
const verify = async () => {
|
||||
if (DUMMY_MODE) {
|
||||
// Keep whatever session is already in memory/local storage as-is;
|
||||
// no API round-trip to re-validate it.
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
if (auth) {
|
||||
try {
|
||||
const { data: user } = await getUser();
|
||||
@ -162,15 +90,12 @@ const AuthProvider = ({ children }: PropsWithChildren) => {
|
||||
// Perbarui auth yang sekarang dengan statusbalance
|
||||
saveAuth({
|
||||
...auth,
|
||||
statusbalance: user.role.status_balance
|
||||
});
|
||||
|
||||
const createCacheUser = {
|
||||
name: user.name,
|
||||
email: user.email,
|
||||
username: user.username,
|
||||
role_name: auth.role_name,
|
||||
statusbalance: user.role.status_balance
|
||||
};
|
||||
|
||||
localStorage.setItem('user', JSON.stringify(createCacheUser));
|
||||
@ -181,20 +106,7 @@ const AuthProvider = ({ children }: PropsWithChildren) => {
|
||||
}
|
||||
};
|
||||
|
||||
// Ensures `loading` always resolves, even if no external wrapper calls
|
||||
// verify() on app start. Safe to call twice if such a wrapper does exist.
|
||||
useEffect(() => {
|
||||
verify();
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, []);
|
||||
|
||||
const logout = async () => {
|
||||
if (DUMMY_MODE) {
|
||||
saveAuth(undefined);
|
||||
setCurrentUser(undefined);
|
||||
return;
|
||||
}
|
||||
|
||||
const createActivity = {
|
||||
module: 'Logout',
|
||||
description: `Logout`,
|
||||
@ -216,9 +128,6 @@ const AuthProvider = ({ children }: PropsWithChildren) => {
|
||||
currentUser,
|
||||
setCurrentUser,
|
||||
login,
|
||||
requestPasswordResetLink,
|
||||
changePassword,
|
||||
getUser,
|
||||
logout,
|
||||
verify
|
||||
}}
|
||||
@ -228,4 +137,4 @@ const AuthProvider = ({ children }: PropsWithChildren) => {
|
||||
);
|
||||
};
|
||||
|
||||
export { AuthContext, AuthProvider };
|
||||
export { AuthContext, AuthProvider };
|
||||
|
||||
Reference in New Issue
Block a user