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