@@ -16,52 +16,6 @@ export const googleClientId = env.PUBLIC_GOOGLE_APPID;
16
16
export const microsoftClientId = env . PUBLIC_MICROSOFT_APPID ;
17
17
export const enableOAuthLogin = facebookClientId || gitHubClientId || googleClientId || microsoftClientId ;
18
18
19
- export async function login ( email : string , password : string ) {
20
- const data : Login = { email, password } ;
21
- const client = useFetchClient ( ) ;
22
- const response = await client . postJSON < TokenResult > ( 'auth/login' , data , {
23
- expectedStatusCodes : [ 401 , 422 ]
24
- } ) ;
25
-
26
- if ( response . ok && response . data ?. token ) {
27
- accessToken . value = response . data . token ;
28
- } else if ( response . status === 401 ) {
29
- response . problem . setErrorMessage ( 'Invalid email or password' ) ;
30
- }
31
-
32
- return response ;
33
- }
34
-
35
- export async function gotoLogin ( ) {
36
- const { url } = get ( page ) ;
37
- const isAuthPath = url . pathname . startsWith ( '/next/login' ) || url . pathname . startsWith ( '/next/logout' ) ;
38
- const redirect = url . pathname === '/next/' || isAuthPath ? '/next/login' : `/next/login?redirect=${ url . pathname } ` ;
39
- await goto ( redirect , { replaceState : true } ) ;
40
- }
41
-
42
- export async function logout ( ) {
43
- const client = useFetchClient ( ) ;
44
- await client . get ( 'auth/logout' , { expectedStatusCodes : [ 200 , 401 ] } ) ;
45
- accessToken . value = null ;
46
- }
47
-
48
- export async function liveLogin ( redirectUrl ?: string ) {
49
- if ( ! microsoftClientId ) {
50
- throw new Error ( 'Live client id not set' ) ;
51
- }
52
-
53
- await oauthLogin ( {
54
- authUrl : 'https://login.live.com/oauth20_authorize.srf' ,
55
- clientId : microsoftClientId ,
56
- extraParams : {
57
- display : 'popup'
58
- } ,
59
- provider : 'live' ,
60
- redirectUrl,
61
- scope : 'wl.emails'
62
- } ) ;
63
- }
64
-
65
19
export async function facebookLogin ( redirectUrl ?: string ) {
66
20
if ( ! facebookClientId ) {
67
21
throw new Error ( 'Facebook client id not set' ) ;
@@ -76,6 +30,21 @@ export async function facebookLogin(redirectUrl?: string) {
76
30
} ) ;
77
31
}
78
32
33
+ export async function githubLogin ( redirectUrl ?: string ) {
34
+ if ( ! gitHubClientId ) {
35
+ throw new Error ( 'GitHub client id not set' ) ;
36
+ }
37
+
38
+ await oauthLogin ( {
39
+ authUrl : 'https://github.com/login/oauth/authorize' ,
40
+ clientId : gitHubClientId ,
41
+ popupOptions : { height : 618 , width : 1020 } ,
42
+ provider : 'github' ,
43
+ redirectUrl,
44
+ scope : 'user:email'
45
+ } ) ;
46
+ }
47
+
79
48
export async function googleLogin ( redirectUrl ?: string ) {
80
49
if ( ! googleClientId ) {
81
50
throw new Error ( 'Google client id not set' ) ;
@@ -97,19 +66,50 @@ export async function googleLogin(redirectUrl?: string) {
97
66
} ) ;
98
67
}
99
68
100
- export async function githubLogin ( redirectUrl ?: string ) {
101
- if ( ! gitHubClientId ) {
102
- throw new Error ( 'GitHub client id not set' ) ;
69
+ export async function gotoLogin ( ) {
70
+ const { url } = get ( page ) ;
71
+ const isAuthPath = url . pathname . startsWith ( '/next/login' ) || url . pathname . startsWith ( '/next/logout' ) ;
72
+ const redirect = url . pathname === '/next/' || isAuthPath ? '/next/login' : `/next/login?redirect=${ url . pathname } ` ;
73
+ await goto ( redirect , { replaceState : true } ) ;
74
+ }
75
+
76
+ export async function liveLogin ( redirectUrl ?: string ) {
77
+ if ( ! microsoftClientId ) {
78
+ throw new Error ( 'Live client id not set' ) ;
103
79
}
104
80
105
81
await oauthLogin ( {
106
- authUrl : 'https://github.com/login/oauth/authorize' ,
107
- clientId : gitHubClientId ,
108
- popupOptions : { height : 618 , width : 1020 } ,
109
- provider : 'github' ,
82
+ authUrl : 'https://login.live.com/oauth20_authorize.srf' ,
83
+ clientId : microsoftClientId ,
84
+ extraParams : {
85
+ display : 'popup'
86
+ } ,
87
+ provider : 'live' ,
110
88
redirectUrl,
111
- scope : 'user:email'
89
+ scope : 'wl.emails'
90
+ } ) ;
91
+ }
92
+
93
+ export async function login ( email : string , password : string ) {
94
+ const data : Login = { email, password } ;
95
+ const client = useFetchClient ( ) ;
96
+ const response = await client . postJSON < TokenResult > ( 'auth/login' , data , {
97
+ expectedStatusCodes : [ 401 , 422 ]
112
98
} ) ;
99
+
100
+ if ( response . ok && response . data ?. token ) {
101
+ accessToken . value = response . data . token ;
102
+ } else if ( response . status === 401 ) {
103
+ response . problem . setErrorMessage ( 'Invalid email or password' ) ;
104
+ }
105
+
106
+ return response ;
107
+ }
108
+
109
+ export async function logout ( ) {
110
+ const client = useFetchClient ( ) ;
111
+ await client . get ( 'auth/logout' , { expectedStatusCodes : [ 200 , 401 ] } ) ;
112
+ accessToken . value = null ;
113
113
}
114
114
115
115
async function oauthLogin ( options : {
@@ -163,6 +163,16 @@ async function oauthLogin(options: {
163
163
}
164
164
}
165
165
166
+ function stringifyOptions ( options : object ) : string {
167
+ const parts = [ ] ;
168
+
169
+ for ( const [ key , value ] of Object . entries ( options ) ) {
170
+ parts . push ( key + '=' + value ) ;
171
+ }
172
+
173
+ return parts . join ( ',' ) ;
174
+ }
175
+
166
176
function waitForUrl ( popup : Window , redirectUri : string ) : Promise < { code : string ; state : string } > {
167
177
return new Promise ( ( resolve , reject ) => {
168
178
const polling = setInterval ( ( ) => {
@@ -207,13 +217,3 @@ function waitForUrl(popup: Window, redirectUri: string): Promise<{ code: string;
207
217
} , 500 ) ;
208
218
} ) ;
209
219
}
210
-
211
- function stringifyOptions ( options : object ) : string {
212
- const parts = [ ] ;
213
-
214
- for ( const [ key , value ] of Object . entries ( options ) ) {
215
- parts . push ( key + '=' + value ) ;
216
- }
217
-
218
- return parts . join ( ',' ) ;
219
- }
0 commit comments