Update Add user hospital portal
This commit is contained in:
@@ -14,6 +14,9 @@ use Modules\Internal\Emails\SendVerifyEmail;
|
||||
use Modules\Internal\Events\ForgetPassword;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Modules\HospitalPortal\Helpers\ApiResponse;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use App\Helpers\Helper;
|
||||
use Illuminate\Support\Facades\View;
|
||||
|
||||
class AuthController extends Controller
|
||||
{
|
||||
@@ -27,9 +30,9 @@ class AuthController extends Controller
|
||||
'email' => 'required|email',
|
||||
'password' => 'required'
|
||||
], [
|
||||
'email.required' => trans('validation.required',['attribute' => 'Email']),
|
||||
'email.email' => trans('validation.email'),
|
||||
'password.required' => trans('validation.required',['attribute' => 'Password']),
|
||||
'email.required' => trans('Validation.required',['attribute' => 'Email']),
|
||||
'email.email' => trans('Validation.email'),
|
||||
'password.required' => trans('Validation.required',['attribute' => 'Password']),
|
||||
]);
|
||||
|
||||
if ($validator->fails())
|
||||
@@ -40,11 +43,11 @@ class AuthController extends Controller
|
||||
{
|
||||
$user = User::where('email', $request->email)->first();
|
||||
if (!$user) {
|
||||
return ApiResponse::apiResponse('Not Found', $data, trans('message.not_found'), 404);
|
||||
return ApiResponse::apiResponse('Not Found', $data, trans('Message.not_found'), 404);
|
||||
}
|
||||
|
||||
if (!Hash::check($request->password, $user->password)) {
|
||||
return ApiResponse::apiResponse('Bad Request', $data, trans('message.password'), 400);
|
||||
return ApiResponse::apiResponse('Bad Request', $data, trans('Message.password'), 400);
|
||||
}
|
||||
|
||||
$res_data = [
|
||||
@@ -52,16 +55,15 @@ class AuthController extends Controller
|
||||
'token' => $user->createToken('app')->plainTextToken
|
||||
];
|
||||
|
||||
return ApiResponse::apiResponse("Success", $res_data, trans('message.success'), 200);
|
||||
return ApiResponse::apiResponse("Success", $res_data, trans('Message.success'), 200);
|
||||
}
|
||||
}
|
||||
|
||||
public function logout(Request $request)
|
||||
{
|
||||
$token = $request->bearerToken();
|
||||
Auth::user()->tokens()->where('id', $token)->delete();
|
||||
$request->user()->tokens()->delete();
|
||||
|
||||
return response(['message' => 'Berhasil Logout.']);
|
||||
return ApiResponse::apiResponse('Success', [], trans('Message.logout'), 200);
|
||||
}
|
||||
|
||||
public function resetPassword(Request $request)
|
||||
@@ -75,12 +77,12 @@ class AuthController extends Controller
|
||||
]);
|
||||
|
||||
if (!Hash::check($request['old_password'], $user->password)) {
|
||||
return response(['message' => 'Password Salah'], 403);
|
||||
return response(['Message' => 'Password Salah'], 403);
|
||||
}
|
||||
|
||||
if ($request["new_password"] != $request["confirm_new_password"]) {
|
||||
return response([
|
||||
'message' => "Password Tidak Sama"
|
||||
'Message' => "Password Tidak Sama"
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -101,7 +103,7 @@ class AuthController extends Controller
|
||||
->first();
|
||||
|
||||
if (!$user) {
|
||||
return response(['message' => 'User Tidak Ditemukan'], 404);
|
||||
return response(['Message' => 'User Tidak Ditemukan'], 404);
|
||||
}
|
||||
|
||||
Event(new ForgetPassword($user));
|
||||
@@ -111,33 +113,77 @@ class AuthController extends Controller
|
||||
return response()->json($user);
|
||||
}
|
||||
|
||||
public function forgetPassword(Request $request)
|
||||
public function forgotPassword(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'new_password' => 'required',
|
||||
'confirm_new_password' => 'required'
|
||||
$data = [
|
||||
'email' => $request->email,
|
||||
];
|
||||
|
||||
$validator = Validator::make($request->all(), [
|
||||
'email' => 'required|email',
|
||||
], [
|
||||
'email.required' => trans('Validation.required',['attribute' => 'Email']),
|
||||
'email.email' => trans('Validation.email'),
|
||||
]);
|
||||
|
||||
$token = Crypt::decryptString($request->token);
|
||||
$email = explode('|', $token)[0];
|
||||
|
||||
$user = User::query()
|
||||
->where('email', $email)
|
||||
->first();
|
||||
|
||||
if (!$user) {
|
||||
return response(['message' => 'User Tidak Ditemukan'], 404);
|
||||
if ($validator->fails())
|
||||
{
|
||||
return ApiResponse::apiResponse('Bad Request', $data, $validator->errors(), 400);
|
||||
}
|
||||
else
|
||||
{
|
||||
$user = User::where('email', $request->email)->first();
|
||||
if (!$user) {
|
||||
return ApiResponse::apiResponse('Not Found', $data, trans('Message.not_found'), 404);
|
||||
}
|
||||
|
||||
if ($request["new_password"] != $request["confirm_new_password"]) {
|
||||
return response([
|
||||
'message' => "Password Tidak Sama"
|
||||
], 404);
|
||||
//send email
|
||||
// Insert data notifications
|
||||
$emailTo = $request->email;
|
||||
$dataNotif = [
|
||||
'user_id' => $user->id,
|
||||
'email' => $emailTo,
|
||||
'title' => 'Forgot Password',
|
||||
'description' => 'Request forgot password from App Doctor',
|
||||
'type' => 1,
|
||||
'isUnRead' => true,
|
||||
'created_by' => auth()->check() ? auth()->user()->id : null,
|
||||
'created_at' => date('Y-m-d H:i:s'),
|
||||
'updated_at' => date('Y-m-d H:i:s'),
|
||||
];
|
||||
$sendNotif = Helper::insertNotification($dataNotif);
|
||||
//Insert data password reset
|
||||
$token = mt_rand(100000, 999999); // Menghasilkan angka acak antara 100000 dan 999999
|
||||
$p_resets = DB::table('password_resets')
|
||||
->insert([
|
||||
'email' => $request->email,
|
||||
'token' => $token,
|
||||
'created_at' => date('Y-m-d H:i:s'),
|
||||
]);
|
||||
// Send Email after insert notifications
|
||||
if($sendNotif && $p_resets)
|
||||
{
|
||||
//send to alarm
|
||||
$nameTo = 'User';
|
||||
$dataEmail = [
|
||||
'email' => $emailTo,
|
||||
'name' => $nameTo,
|
||||
'subject' => 'Request Forgot Password from App Doctor Date '. date('Y-m-d H:i:s'),
|
||||
'body' => View::make('email/forgot_password', ['token' => $token])->render(),
|
||||
];
|
||||
Helper::sendEmail($dataEmail);
|
||||
|
||||
$res = DB::table('password_resets')
|
||||
->where('email', '=', $request->email)
|
||||
->where('token', '=', $token)
|
||||
->get();
|
||||
|
||||
return ApiResponse::apiResponse("Success", $res, trans('Message.success'), 200);
|
||||
}
|
||||
else
|
||||
{
|
||||
return ApiResponse::apiResponse("Internal Server Error", $data, trans('Message.server_error'), 500);
|
||||
}
|
||||
}
|
||||
|
||||
$user->update([
|
||||
'password' => Hash::make($request->confirm_new_password),
|
||||
]);
|
||||
return response()->json($user);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,13 +24,15 @@ Route::prefix('v1')->group(function() {
|
||||
Route::prefix('hospitalportal')->group(function () {
|
||||
|
||||
Route::middleware(Authentication::class)->group(function () {
|
||||
Route::controller(AuthController::class)->group(function () {
|
||||
Route::post('login', 'login');
|
||||
Route::middleware('switch.db')->group(function () {
|
||||
Route::controller(AuthController::class)->group(function () {
|
||||
Route::post('login', 'login');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
//Route::post('forget-password', [AuthController::class, 'forgetPassword'])->name('forget-password');
|
||||
//Route::post('verify-email', [AuthController::class, 'verifyEmail'])->name('verify-email');
|
||||
|
||||
Route::post('forgot-password', [AuthController::class, 'forgotPassword']);
|
||||
// Route::post('verify-email', [AuthController::class, 'verifyEmail'])->name('verify-email');
|
||||
|
||||
|
||||
Route::middleware('auth:sanctum')->group(function () {
|
||||
|
||||
@@ -206,7 +206,7 @@ class AppServiceProvider extends ServiceProvider
|
||||
$this->logAuditTrail($model, 'deleted');
|
||||
});
|
||||
|
||||
Sanctum::usePersonalAccessTokenModel(PersonalAccessToken::class);
|
||||
// Sanctum::usePersonalAccessTokenModel(PersonalAccessToken::class);
|
||||
}
|
||||
|
||||
private function logAuditTrail($model, $action)
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
"txtDialogMember3" : "Detail",
|
||||
"txtDialogMember4" : "Please select services",
|
||||
"txtDialogMember5" : "Admission Date",
|
||||
"txtDialogMember6" : "Please select admission date",
|
||||
"txtDialogMember6" : "Please select admission date",
|
||||
"txtWarningDischargeDate" : "Please select discharge date",
|
||||
"txtCreateAt" : "Create at",
|
||||
"txtDateBirth" : "Date of Birth",
|
||||
@@ -59,5 +59,6 @@
|
||||
"txtApprove": "Approve",
|
||||
"txtDialogConfirmation": "Are you sure you want to proceed with this action?",
|
||||
"txtStartDate": "Start Date",
|
||||
"txtEndDate": "End Date"
|
||||
"txtEndDate": "End Date",
|
||||
"txtHelp1" : "Has problem with your account?"
|
||||
}
|
||||
|
||||
@@ -59,5 +59,6 @@
|
||||
"txtApprove": "Terima",
|
||||
"txtDialogConfirmation": "Apakah Anda yakin ingin melanjutkan tindakan ini?",
|
||||
"txtStartDate": "Tanggal Mulai",
|
||||
"txtEndDate": "Tanggal Akhir"
|
||||
"txtEndDate": "Tanggal Akhir",
|
||||
"txtHelp1" : "Punya masalah dengan akun Anda?"
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ export type JWTContextType = {
|
||||
isInitialized: boolean;
|
||||
user: AuthUser;
|
||||
method: 'jwt';
|
||||
login: (email: string, password: string) => Promise<void>;
|
||||
login: (email: string, password: string, rememberMe: boolean) => Promise<void>;
|
||||
register: (email: string, password: string, firstName: string, lastName: string) => Promise<void>;
|
||||
logout: () => Promise<void>;
|
||||
};
|
||||
|
||||
@@ -93,7 +93,7 @@ export default function Table<T>({
|
||||
]);
|
||||
params.setAppliedParams(parameters);
|
||||
};
|
||||
|
||||
|
||||
const { localeData }: any = useContext(LanguageContext);
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
@@ -106,7 +106,7 @@ export default function Table<T>({
|
||||
return (
|
||||
<TableHead>
|
||||
<TableRow>
|
||||
{selected.useSelected && selected.selectedRows.length > 0 ? (
|
||||
{selected.useSelected && selected.selectedRows.length > 0 ? (
|
||||
<>
|
||||
<TableCell style={{ backgroundColor: '#D1F1F1', }} align="left" colSpan={selected.totRows} sx={{ padding: 2 }}>
|
||||
<Grid container alignItems="center" justifyContent="space-between">
|
||||
@@ -169,10 +169,10 @@ export default function Table<T>({
|
||||
</TableCell>
|
||||
))}
|
||||
</>
|
||||
|
||||
|
||||
)}
|
||||
|
||||
|
||||
|
||||
|
||||
</TableRow>
|
||||
</TableHead>
|
||||
);
|
||||
@@ -294,7 +294,7 @@ export default function Table<T>({
|
||||
</form>
|
||||
</Grid>
|
||||
}
|
||||
|
||||
|
||||
</Fragment>
|
||||
) : null }
|
||||
|
||||
@@ -380,7 +380,7 @@ export default function Table<T>({
|
||||
</Select>
|
||||
</FormControl>
|
||||
</Grid>
|
||||
) : null }
|
||||
) : null }
|
||||
|
||||
{/* Export Report */}
|
||||
|
||||
@@ -389,11 +389,11 @@ export default function Table<T>({
|
||||
<FormControl fullWidth>
|
||||
<Button variant='contained' sx={{p:2}}>
|
||||
<Download />
|
||||
<Typography variant='inherit' sx={{marginLeft: 1}}>Export</Typography>
|
||||
<Typography variant='inherit' sx={{marginLeft: 1}}>Export</Typography>
|
||||
</Button>
|
||||
</FormControl>
|
||||
</Grid>
|
||||
) : null }
|
||||
) : null }
|
||||
|
||||
</Grid>
|
||||
</Grid>
|
||||
@@ -428,7 +428,7 @@ export default function Table<T>({
|
||||
</TableCell>
|
||||
):(
|
||||
<TableCell>
|
||||
|
||||
|
||||
</TableCell>
|
||||
))}
|
||||
{headCells &&
|
||||
@@ -443,7 +443,7 @@ export default function Table<T>({
|
||||
))
|
||||
) : (
|
||||
<TableRow>
|
||||
<TableCell colSpan={6} align="center">
|
||||
<TableCell colSpan={headCells?.length} align="center">
|
||||
{localeData.txtDataNotFound}
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
|
||||
@@ -2,7 +2,7 @@ import { createContext, ReactNode, useEffect, useReducer } from 'react';
|
||||
// utils
|
||||
import axios from '@/utils/axios';
|
||||
// import { isValidToken, setSession } from '@/utils/jwt';
|
||||
import { setSession, getSession, setUser, getUser } from '@/utils/token';
|
||||
import { setSession, getSession, setUser, getUser, getCookie } from '@/utils/token';
|
||||
// @types
|
||||
import { ActionMap, AuthState, AuthUser, JWTContextType } from '@/@types/auth';
|
||||
// ----------------------------------------------------------------------
|
||||
@@ -86,12 +86,16 @@ function AuthProvider({ children }: AuthProviderProps) {
|
||||
const initialize = async () => {
|
||||
try {
|
||||
const accessToken = getSession();
|
||||
if (accessToken) {
|
||||
setSession(accessToken);
|
||||
const rememberMe = getCookie('rememberMe') == 'OK' ? false : true;
|
||||
|
||||
if (accessToken) {
|
||||
const userString = getUser();
|
||||
const storedUser = userString ? JSON.parse(userString) : null;
|
||||
setUser(storedUser, rememberMe);
|
||||
setSession(accessToken, rememberMe);
|
||||
const response = await axios.get('/user');
|
||||
const user = response.data;
|
||||
|
||||
const response = await axios.get('/user');
|
||||
const user = response.data;
|
||||
|
||||
dispatch({
|
||||
type: Types.Initial,
|
||||
payload: {
|
||||
@@ -126,16 +130,16 @@ function AuthProvider({ children }: AuthProviderProps) {
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'Content-Type' : 'application/json',
|
||||
'Accept-Language': (localStorage.getItem('currentLocale') ? localStorage.getItem('currentLocale') : 'id-ID'),
|
||||
'Accept-Language': localStorage.getItem('currentLocale') ?? 'id-ID',
|
||||
},
|
||||
};
|
||||
|
||||
const login = async (email: string, password: string) => axios
|
||||
const login = async (email: string, password: string, rememberMe: boolean) => axios
|
||||
.post('/login', { email, password }, headers)
|
||||
.then((response) => {
|
||||
const { user, token } = response.data.data;
|
||||
setSession(token);
|
||||
setUser(user);
|
||||
setSession(token, rememberMe);
|
||||
setUser(user, rememberMe);
|
||||
|
||||
dispatch({
|
||||
type: Types.Login,
|
||||
@@ -168,8 +172,9 @@ function AuthProvider({ children }: AuthProviderProps) {
|
||||
};
|
||||
|
||||
const logout = async () => {
|
||||
setSession(null);
|
||||
setUser(null);
|
||||
await axios.post('logout');
|
||||
setSession(null, false);
|
||||
setUser(null, false);
|
||||
dispatch({ type: Types.Logout });
|
||||
};
|
||||
|
||||
@@ -187,9 +192,9 @@ function AuthProvider({ children }: AuthProviderProps) {
|
||||
);
|
||||
|
||||
// if (state.isInitialized) {
|
||||
// return (!state.isAuthenticated && location.pathname !== '/auth/login') ?
|
||||
// return (!state.isAuthenticated && location.pathname !== '/auth/login') ?
|
||||
// (<Navigate to="/auth/login" replace={true} />)
|
||||
// : false && location.pathname == '/auth/login' ?
|
||||
// : false && location.pathname == '/auth/login' ?
|
||||
// (<Navigate to="/dashboard" replace={true} />)
|
||||
// : (
|
||||
// <AuthContext.Provider
|
||||
|
||||
@@ -8,6 +8,8 @@ import { IconButtonAnimate } from '@/components/animate';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import useAuth from '@/hooks/useAuth';
|
||||
|
||||
import { getUser } from '@/utils/token';
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
const MENU_OPTIONS = [
|
||||
@@ -45,6 +47,8 @@ export default function AccountPopover() {
|
||||
navigate('/auth/login');
|
||||
};
|
||||
|
||||
const userString = getUser();
|
||||
const storedUser = userString ? JSON.parse(userString) : null;
|
||||
return (
|
||||
<>
|
||||
<IconButtonAnimate
|
||||
@@ -89,7 +93,7 @@ export default function AccountPopover() {
|
||||
Hospital Admin
|
||||
</Typography>
|
||||
<Typography variant="body2" sx={{ color: 'text.secondary' }} noWrap>
|
||||
hospitaladmin@gmail.com
|
||||
{storedUser?.email}
|
||||
</Typography>
|
||||
</Box>
|
||||
|
||||
|
||||
@@ -78,29 +78,28 @@ export default function Login() {
|
||||
const smUp = useResponsive("up", "sm");
|
||||
|
||||
const mdUp = useResponsive("up", "md");
|
||||
const handleClick = () => {
|
||||
window.location.href = 'https://wa.me/6285890008500';
|
||||
};
|
||||
|
||||
return (
|
||||
<Page title="Login">
|
||||
<RootStyle>
|
||||
<HeaderStyle>
|
||||
{/*<Logo sx={{ width: 150, height: 150 }} />
|
||||
<Logo sx={{ width: 150, height: 150, display: 'none' }} />
|
||||
{smUp && (
|
||||
<Typography variant="body2" sx={{ mt: { md: -2 } }}>
|
||||
Has problem with your account? {""}
|
||||
{localeData.txtHelp1} {""}
|
||||
<Link
|
||||
variant="subtitle2"
|
||||
component={RouterLink}
|
||||
to="#"
|
||||
onClick={(e) => {
|
||||
window.location.href =
|
||||
"mailto:admin@linksehat.com";
|
||||
e.preventDefault();
|
||||
}}
|
||||
onClick={handleClick}
|
||||
>
|
||||
Contact Us
|
||||
{localeData.txtContactUs}
|
||||
</Link>
|
||||
</Typography>
|
||||
)}*/}
|
||||
)}
|
||||
</HeaderStyle>
|
||||
|
||||
{/* {mdUp && (
|
||||
@@ -116,7 +115,7 @@ export default function Login() {
|
||||
/>
|
||||
</SectionStyle>
|
||||
)} */}
|
||||
|
||||
|
||||
<Container maxWidth="sm">
|
||||
<ContentStyle>
|
||||
<Card sx={{padding:2}}>
|
||||
@@ -125,7 +124,7 @@ export default function Login() {
|
||||
alignItems="center"
|
||||
sx={{ mb: 5 }}
|
||||
>
|
||||
|
||||
|
||||
<Logo sx={{ width: 90, height: 90 }} />
|
||||
<Box sx={{ flexGrow: 1 }}>
|
||||
<Typography variant="h4" gutterBottom>
|
||||
|
||||
@@ -52,7 +52,7 @@ export default function Router() {
|
||||
},
|
||||
// { path: 'login-unprotected', element: <Login /> },
|
||||
// { path: 'register-unprotected', element: <Register /> },
|
||||
{ path: 'reset-password', element: <ResetPassword /> },
|
||||
{ path: 'forgot-password', element: <ResetPassword /> },
|
||||
{ path: 'forget-password', element: <ForgetPassword /> },
|
||||
// { path: 'verify', element: <VerifyCode /> },
|
||||
],
|
||||
@@ -117,7 +117,7 @@ export default function Router() {
|
||||
}
|
||||
|
||||
const Login = Loadable(lazy(() => import('@/pages/auth/Login')));
|
||||
const ResetPassword = Loadable(lazy(() => import('@/pages/auth/ResetPassword')));
|
||||
const ResetPassword = Loadable(lazy(() => import('@/pages/auth/VerifyCode')));
|
||||
const ForgetPassword = Loadable(lazy(() => import('@/pages/auth/ForgetPassword')));
|
||||
|
||||
// Dashboard
|
||||
|
||||
@@ -12,5 +12,5 @@ export const PATH_AUTH = {
|
||||
loginUnprotected: path(ROOTS_AUTH, '/login-unprotected'),
|
||||
registerUnprotected: path(ROOTS_AUTH, '/register-unprotected'),
|
||||
verify: path(ROOTS_AUTH, '/verify'),
|
||||
resetPassword: path(ROOTS_AUTH, '/reset-password'),
|
||||
resetPassword: path(ROOTS_AUTH, '/forgot-password'),
|
||||
};
|
||||
|
||||
@@ -60,7 +60,7 @@ export default function LoginForm() {
|
||||
|
||||
const onSubmit = async (data: FormValuesProps) => {
|
||||
try {
|
||||
const loginResult = await login(data.email, data.password);
|
||||
const loginResult = await login(data.email, data.password, data.remember);
|
||||
|
||||
navigate('/dashboard');
|
||||
} catch (error) {
|
||||
@@ -100,10 +100,10 @@ export default function LoginForm() {
|
||||
</Stack>
|
||||
|
||||
<Stack direction="row" alignItems="center" justifyContent="space-between" sx={{ my: 2 }}>
|
||||
{/*<RHFCheckbox name="remember" label="Remember me" />
|
||||
<RHFCheckbox name="remember" label="Remember me"/>
|
||||
<Link component={RouterLink} variant="subtitle2" to={PATH_AUTH.resetPassword}>
|
||||
Forgot password?
|
||||
</Link>*/}
|
||||
</Link>
|
||||
</Stack>
|
||||
|
||||
<LoadingButton
|
||||
|
||||
@@ -25,34 +25,97 @@ import axios from './axios';
|
||||
// }, timeLeft);
|
||||
// };
|
||||
|
||||
const setSession = (accessToken: string | null) => {
|
||||
let expiredCookie = '12 * 60';
|
||||
|
||||
const setCookie = (name:any, value:any, days:any) => {
|
||||
let expires = "";
|
||||
if (days) {
|
||||
const date = new Date();
|
||||
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
|
||||
expires = "; expires=" + date.toUTCString();
|
||||
}
|
||||
document.cookie = name + "=" + decodeURIComponent(value || "") + expires + "; path=/; SameSite=Strict";
|
||||
};
|
||||
|
||||
const setSession = (accessToken: string | null, rememberMe: boolean) => {
|
||||
if (accessToken) {
|
||||
localStorage.setItem('accessToken', accessToken);
|
||||
const userString = getUser();
|
||||
const storedUser = userString ? JSON.parse(userString) : null;
|
||||
if(rememberMe)
|
||||
{
|
||||
localStorage.setItem('accessToken', accessToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
setCookie('accessToken', accessToken, expiredCookie);
|
||||
setCookie('rememberMe', 'OK', expiredCookie);
|
||||
}
|
||||
|
||||
axios.defaults.headers.common.Authorization = `Bearer ${accessToken}`;
|
||||
axios.defaults.headers.common['Accept-Language'] = (localStorage.getItem('currentLocale') ? localStorage.getItem('currentLocale') : 'id-ID');
|
||||
axios.defaults.headers.common['Accept-Language'] = localStorage.getItem('currentLocale') ?? 'id-ID';
|
||||
axios.defaults.headers.common['Accept'] = 'application/json';
|
||||
axios.defaults.headers.common['Content-Type'] = 'application/json';
|
||||
axios.defaults.headers.common['Organization-id'] = storedUser?.organization_id;
|
||||
// This function below will handle when token is expired
|
||||
// const { exp } = jwtDecode(accessToken);
|
||||
// handleTokenExpired(exp);
|
||||
} else {
|
||||
localStorage.removeItem('accessToken');
|
||||
removeCookie('accessToken');
|
||||
removeCookie('rememberMe');
|
||||
delete axios.defaults.headers.common.Authorization;
|
||||
delete axios.defaults.headers.common['Accept-Language'];
|
||||
delete axios.defaults.headers.common['Accept'];
|
||||
delete axios.defaults.headers.common['Content-Type'];
|
||||
delete axios.defaults.headers.common['Content-Type'];
|
||||
}
|
||||
};
|
||||
|
||||
const setUser = (user: any) => {
|
||||
const setUser = (user: any, rememberMe: boolean) => {
|
||||
if (user) {
|
||||
localStorage.setItem('user', user);
|
||||
if(rememberMe)
|
||||
{
|
||||
localStorage.setItem('user', JSON.stringify(user));
|
||||
}
|
||||
else
|
||||
{
|
||||
setCookie('user', JSON.stringify(user), expiredCookie);
|
||||
setCookie('rememberMe', 'OK', expiredCookie);
|
||||
}
|
||||
|
||||
} else {
|
||||
localStorage.removeItem('user');
|
||||
removeCookie('user');
|
||||
removeCookie('rememberMe');
|
||||
}
|
||||
};
|
||||
|
||||
const getSession = () => window.localStorage.getItem('accessToken')
|
||||
const getUser = () => window.localStorage.getItem('user')
|
||||
const getCookie = (name:any) => {
|
||||
const cookies = document.cookie.split('; ');
|
||||
for (let i = 0; i < cookies.length; i++) {
|
||||
const cookiePair = cookies[i].split('=');
|
||||
if (cookiePair[0] === name) {
|
||||
return decodeURIComponent(cookiePair[1]);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
export { setSession, getSession, setUser, getUser };
|
||||
const getSession = () => {
|
||||
const localToken = window.localStorage.getItem('accessToken');
|
||||
const cookieToken = getCookie('accessToken');
|
||||
// Prioritaskan token dari localStorage
|
||||
return localToken || cookieToken;
|
||||
};
|
||||
// const getUser = () => window.localStorage.getItem('user') || window.sessionStorage.getItem('user')
|
||||
const getUser = () => {
|
||||
const localUser = window.localStorage.getItem('user');
|
||||
const cookieUser = getCookie('user');
|
||||
|
||||
// Prioritaskan token dari localStorage
|
||||
return localUser || cookieUser;
|
||||
};
|
||||
const removeCookie = (name:any) => {
|
||||
document.cookie = name + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
|
||||
};
|
||||
|
||||
export { setSession, getSession, setUser, getUser, getCookie };
|
||||
|
||||
Reference in New Issue
Block a user