11import { useUserContext } from "@/context/user-context" ;
2- import { useQuery } from "@tanstack/react-query" ;
3- import { Navigate } from "react-router" ;
2+ import { useMutation , useQuery } from "@tanstack/react-query" ;
3+ import { Navigate , useNavigate } from "react-router" ;
44import { useLocation } from "react-router" ;
55import {
66 Card ,
@@ -11,6 +11,8 @@ import {
1111} from "@/components/ui/card" ;
1212import { getOidcClientInfoScehma } from "@/schemas/oidc-schemas" ;
1313import { Button } from "@/components/ui/button" ;
14+ import axios from "axios" ;
15+ import { toast } from "sonner" ;
1416
1517type AuthorizePageProps = {
1618 scope : string ;
@@ -25,6 +27,7 @@ const optionalAuthorizeProps = ["state"];
2527export const AuthorizePage = ( ) => {
2628 const { isLoggedIn } = useUserContext ( ) ;
2729 const { search } = useLocation ( ) ;
30+ const navigate = useNavigate ( ) ;
2831
2932 const searchParams = new URLSearchParams ( search ) ;
3033
@@ -46,20 +49,46 @@ export const AuthorizePage = () => {
4649 } ,
4750 } ) ;
4851
52+ const authorizeMutation = useMutation ( {
53+ mutationFn : ( ) => {
54+ return axios . post ( "/api/oidc/authorize" , {
55+ scope : props . scope ,
56+ response_type : props . responseType ,
57+ client_id : props . clientId ,
58+ redirect_uri : props . redirectUri ,
59+ state : props . state ,
60+ } ) ;
61+ } ,
62+ mutationKey : [ "authorize" , props . clientId ] ,
63+ onSuccess : ( data ) => {
64+ toast . info ( "Authorized" , {
65+ description : "You will be soon redirected to your application" ,
66+ } ) ;
67+ window . location . replace (
68+ `${ data . data . redirect_uri } ?code=${ encodeURIComponent ( data . data . code ) } &state=${ encodeURIComponent ( data . data . state ) } ` ,
69+ ) ;
70+ } ,
71+ onError : ( error ) => {
72+ window . location . replace (
73+ `/error?error=${ encodeURIComponent ( error . message ) } ` ,
74+ ) ;
75+ } ,
76+ } ) ;
77+
4978 if ( ! isLoggedIn ) {
5079 // TODO: Pass the params to the login page, so user can login -> authorize
5180 return < Navigate to = "/login" replace /> ;
5281 }
5382
54- for ( const key in Object . keys ( props ) ) {
83+ Object . keys ( props ) . forEach ( ( key ) => {
5584 if (
5685 ! props [ key as keyof AuthorizePageProps ] &&
5786 ! optionalAuthorizeProps . includes ( key )
5887 ) {
5988 // TODO: Add reason for error
6089 return < Navigate to = "/error" replace /> ;
6190 }
62- }
91+ } ) ;
6392
6493 if ( getClientInfo . isLoading ) {
6594 return (
@@ -91,8 +120,19 @@ export const AuthorizePage = () => {
91120 </ CardDescription >
92121 </ CardHeader >
93122 < CardFooter className = "flex flex-col items-stretch gap-2" >
94- < Button > Authorize</ Button >
95- < Button variant = "outline" > Cancel</ Button >
123+ < Button
124+ onClick = { ( ) => authorizeMutation . mutate ( ) }
125+ loading = { authorizeMutation . isPending }
126+ >
127+ Authorize
128+ </ Button >
129+ < Button
130+ onClick = { ( ) => navigate ( "/" ) }
131+ disabled = { authorizeMutation . isPending }
132+ variant = "outline"
133+ >
134+ Cancel
135+ </ Button >
96136 </ CardFooter >
97137 </ Card >
98138 ) ;
0 commit comments