@@ -2,12 +2,14 @@ import type { GetServerSideProps, NextPage } from 'next';
2
2
import { useAuth , useToaster } from '@/hooks' ;
3
3
import { Container } from '@/components/Container' ;
4
4
import { Button } from '@/components/Button' ;
5
- import { SpotifyIcon } from '@/components/Icons' ;
5
+ import { AppleMusicIcon , SpotifyIcon } from '@/components/Icons' ;
6
+ import type { MouseEventHandler } from 'react' ;
6
7
import { useEffect } from 'react' ;
7
8
import { useRouter } from 'next/router' ;
8
9
import Cookies from 'js-cookie' ;
9
10
import type { SSRProps } from '@/utils/ssrUtils' ;
10
11
import { fetchUser } from '@/utils/ssrUtils' ;
12
+ import { LoginAppleMusicButton } from '@/components/Login/LoginAppleMusicButton' ;
11
13
12
14
export const getServerSideProps : GetServerSideProps < SSRProps > = async ( ctx ) => {
13
15
const user = await fetchUser ( ctx ) ;
@@ -34,6 +36,32 @@ const Login: NextPage = () => {
34
36
Cookies . set ( 'redirectUrl' , redirect . toString ( ) ) ;
35
37
} , [ router ] ) ;
36
38
39
+ const generateQueryString = ( q : Record < string , any > ) : string => {
40
+ return Object . entries ( q )
41
+ . filter (
42
+ ( [ _ , value ] ) =>
43
+ value !== undefined &&
44
+ value !== null &&
45
+ value . toString ( ) . trim ( ) !== ''
46
+ )
47
+ . map ( ( [ key , value ] ) => `${ key } =${ encodeURIComponent ( value ) } ` )
48
+ . join ( '&' ) ;
49
+ } ;
50
+
51
+ const appleAuthHandle : MouseEventHandler < HTMLButtonElement > = ( ) => {
52
+ window . location . href = `https://appleid.apple.com/auth/authorize?${ generateQueryString (
53
+ {
54
+ response_type : 'code' ,
55
+ response_mode : 'form_post' ,
56
+ client_id : process . env . NEXT_PUBLIC_APPLE_CLIENT_ID ,
57
+ redirect_uri : process . env . NEXT_PUBLIC_APPLE_REDIRECT_URI
58
+ ? `${ process . env . NEXT_PUBLIC_APPLE_REDIRECT_URI } /web`
59
+ : '' ,
60
+ scope : 'email name' ,
61
+ }
62
+ ) } `;
63
+ } ;
64
+
37
65
return (
38
66
< Container className = "flex min-h-[90vh] pt-24" >
39
67
< div className = "mx-auto mt-48 flex w-96 flex-col px-4" >
@@ -43,14 +71,27 @@ const Login: NextPage = () => {
43
71
< div className = "mt-8 flex flex-col gap-4" >
44
72
< Button
45
73
onClick = { ( ) => {
46
- auth . login ( ) ;
74
+ router . push ( '/api/auth/spotify-login' ) ;
47
75
} }
48
76
className = "w-full bg-primary/80 text-black hover:bg-primary/60 active:bg-primary/50"
49
77
>
50
78
< SpotifyIcon className = "mr-2 !fill-black" />
51
79
Continue with Spotify
52
80
</ Button >
53
81
</ div >
82
+ { auth ?. user ?. appleMusicAuth ? (
83
+ < LoginAppleMusicButton />
84
+ ) : (
85
+ < div className = "mt-8 flex flex-col gap-4" >
86
+ < Button
87
+ onClick = { appleAuthHandle }
88
+ className = "w-full bg-applemusic/80 text-white hover:!bg-applemusic/60 active:!bg-applemusic/50"
89
+ >
90
+ < AppleMusicIcon className = "mr-2 !fill-white" hover = { false } />
91
+ Continue with Apple
92
+ </ Button >
93
+ </ div >
94
+ ) }
54
95
</ div >
55
96
</ Container >
56
97
) ;
0 commit comments