11"use client" ;
22
3- import { ServerConfigContext , ThemeContext } from "@components/contexts" ;
3+ import {
4+ AddressContext ,
5+ ServerConfigContext ,
6+ ThemeContext ,
7+ } from "@components/contexts" ;
48import {
59 Button ,
610 Caption ,
@@ -13,12 +17,7 @@ import {
1317import { useContext , useState } from "react" ;
1418import { FormEvent } from "react" ;
1519import { signIn } from "next-auth/react" ;
16- import {
17- Form ,
18- // FormField,
19- // FormSubmit,
20- useToast ,
21- } from "@courselit/components-library" ;
20+ import { Form , useToast } from "@courselit/components-library" ;
2221import {
2322 BTN_LOGIN ,
2423 BTN_LOGIN_GET_CODE ,
@@ -34,6 +33,10 @@ import Link from "next/link";
3433import { TriangleAlert } from "lucide-react" ;
3534import { useRecaptcha } from "@/hooks/use-recaptcha" ;
3635import RecaptchaScriptLoader from "@/components/recaptcha-script-loader" ;
36+ import { checkPermission } from "@courselit/utils" ;
37+ import { Profile } from "@courselit/common-models" ;
38+ import { getUserProfile } from "../../helpers" ;
39+ import { ADMIN_PERMISSIONS } from "@ui-config/constants" ;
3740
3841export default function LoginForm ( { redirectTo } : { redirectTo ?: string } ) {
3942 const { theme } = useContext ( ThemeContext ) ;
@@ -45,6 +48,7 @@ export default function LoginForm({ redirectTo }: { redirectTo?: string }) {
4548 const { toast } = useToast ( ) ;
4649 const serverConfig = useContext ( ServerConfigContext ) ;
4750 const { executeRecaptcha } = useRecaptcha ( ) ;
51+ const address = useContext ( AddressContext ) ;
4852
4953 const requestCode = async function ( e : FormEvent ) {
5054 e . preventDefault ( ) ;
@@ -152,13 +156,28 @@ export default function LoginForm({ redirectTo }: { redirectTo?: string }) {
152156 if ( response ?. error ) {
153157 setError ( `Can't sign you in at this time` ) ;
154158 } else {
155- window . location . href = redirectTo || "/dashboard/my-content" ;
159+ window . location . href =
160+ redirectTo ||
161+ getRedirectURLBasedOnProfile (
162+ await getUserProfile ( address . backend ) ,
163+ ) ;
156164 }
157165 } finally {
158166 setLoading ( false ) ;
159167 }
160168 } ;
161169
170+ const getRedirectURLBasedOnProfile = ( profile : Profile ) => {
171+ if (
172+ profile ?. userId &&
173+ checkPermission ( profile . permissions ! , ADMIN_PERMISSIONS )
174+ ) {
175+ return "/dashboard/overview" ;
176+ } else {
177+ return "/dashboard/my-content" ;
178+ }
179+ } ;
180+
162181 return (
163182 < Section theme = { theme . theme } >
164183 < div className = "flex flex-col gap-4 min-h-[80vh]" >
@@ -251,10 +270,6 @@ export default function LoginForm({ redirectTo }: { redirectTo?: string }) {
251270 theme = { theme . theme }
252271 />
253272 < div className = "flex justify-center" >
254- { /* <FormSubmit
255- text={loading ? LOADING : BTN_LOGIN}
256- disabled={loading}
257- /> */ }
258273 < Button
259274 theme = { theme . theme }
260275 disabled = { loading }
0 commit comments