@@ -13,37 +13,24 @@ import { getOidcClientInfoScehma } from "@/schemas/oidc-schemas";
1313import { Button } from "@/components/ui/button" ;
1414import axios from "axios" ;
1515import { toast } from "sonner" ;
16-
17- type AuthorizePageProps = {
18- scope : string ;
19- responseType : string ;
20- clientId : string ;
21- redirectUri : string ;
22- state : string ;
23- } ;
24-
25- const optionalAuthorizeProps = [ "state" ] ;
16+ import { useOIDCParams } from "@/lib/hooks/oidc" ;
2617
2718export const AuthorizePage = ( ) => {
2819 const { isLoggedIn } = useUserContext ( ) ;
2920 const { search } = useLocation ( ) ;
3021 const navigate = useNavigate ( ) ;
3122
3223 const searchParams = new URLSearchParams ( search ) ;
33-
34- // If there is a better way to do this, please do let me know
35- const props : AuthorizePageProps = {
36- scope : searchParams . get ( "scope" ) || "" ,
37- responseType : searchParams . get ( "response_type" ) || "" ,
38- clientId : searchParams . get ( "client_id" ) || "" ,
39- redirectUri : searchParams . get ( "redirect_uri" ) || "" ,
40- state : searchParams . get ( "state" ) || "" ,
41- } ;
24+ const {
25+ values : props ,
26+ missingParams,
27+ compiled : compiledOIDCParams ,
28+ } = useOIDCParams ( searchParams ) ;
4229
4330 const getClientInfo = useQuery ( {
44- queryKey : [ "client" , props . clientId ] ,
31+ queryKey : [ "client" , props . client_id ] ,
4532 queryFn : async ( ) => {
46- const res = await fetch ( `/api/oidc/clients/${ props . clientId } ` ) ;
33+ const res = await fetch ( `/api/oidc/clients/${ props . client_id } ` ) ;
4734 const data = await getOidcClientInfoScehma . parseAsync ( await res . json ( ) ) ;
4835 return data ;
4936 } ,
@@ -53,13 +40,13 @@ export const AuthorizePage = () => {
5340 mutationFn : ( ) => {
5441 return axios . post ( "/api/oidc/authorize" , {
5542 scope : props . scope ,
56- response_type : props . responseType ,
57- client_id : props . clientId ,
58- redirect_uri : props . redirectUri ,
43+ response_type : props . response_type ,
44+ client_id : props . client_id ,
45+ redirect_uri : props . redirect_uri ,
5946 state : props . state ,
6047 } ) ;
6148 } ,
62- mutationKey : [ "authorize" , props . clientId ] ,
49+ mutationKey : [ "authorize" , props . client_id ] ,
6350 onSuccess : ( data ) => {
6451 toast . info ( "Authorized" , {
6552 description : "You will be soon redirected to your application" ,
@@ -74,19 +61,17 @@ export const AuthorizePage = () => {
7461 } ) ;
7562
7663 if ( ! isLoggedIn ) {
77- // TODO: Pass the params to the login page, so user can login -> authorize
78- return < Navigate to = "/login" replace /> ;
64+ return < Navigate to = { `/login?${ compiledOIDCParams } ` } replace /> ;
7965 }
8066
81- Object . keys ( props ) . forEach ( ( key ) => {
82- if (
83- ! props [ key as keyof AuthorizePageProps ] &&
84- ! optionalAuthorizeProps . includes ( key )
85- ) {
86- // TODO: Add reason for error
87- return < Navigate to = "/error" replace /> ;
88- }
89- } ) ;
67+ if ( missingParams . length > 0 ) {
68+ return (
69+ < Navigate
70+ to = { `/error?error=${ encodeURIComponent ( `Missing parameters: ${ missingParams . join ( ", " ) } ` ) } ` }
71+ replace
72+ />
73+ ) ;
74+ }
9075
9176 if ( getClientInfo . isLoading ) {
9277 return (
@@ -102,8 +87,12 @@ export const AuthorizePage = () => {
10287 }
10388
10489 if ( getClientInfo . isError ) {
105- // TODO: Add reason for error
106- return < Navigate to = "/error" replace /> ;
90+ return (
91+ < Navigate
92+ to = { `/error?error=${ encodeURIComponent ( `Failed to load client information` ) } ` }
93+ replace
94+ />
95+ ) ;
10796 }
10897
10998 return (
0 commit comments