Skip to content

Commit ad1563f

Browse files
feat: Supabase OAuth (#288)
1 parent 9c67be3 commit ad1563f

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import axios from 'axios';
2+
import qs from 'qs';
3+
import { DataObject, OAuthResponse } from '../../lib/types';
4+
5+
export const init = async ({ body }: DataObject): Promise<OAuthResponse> => {
6+
try {
7+
const {
8+
clientId: client_id,
9+
clientSecret: client_secret,
10+
metadata: { code, redirectUri: redirect_uri },
11+
} = body;
12+
13+
const requestBody = {
14+
grant_type: 'authorization_code',
15+
code,
16+
client_id,
17+
client_secret,
18+
redirect_uri,
19+
};
20+
21+
const response = await axios({
22+
url: 'https://api.supabase.com/v1/oauth/token',
23+
method: 'POST',
24+
headers: {
25+
'Content-Type': 'application/x-www-form-urlencoded',
26+
},
27+
data: qs.stringify(requestBody),
28+
});
29+
30+
const {
31+
data: {
32+
access_token: accessToken,
33+
refresh_token: refreshToken,
34+
token_type: tokenType,
35+
expires_in: expiresIn,
36+
},
37+
} = response;
38+
39+
return {
40+
accessToken,
41+
refreshToken,
42+
expiresIn,
43+
tokenType,
44+
meta: {},
45+
};
46+
} catch (error) {
47+
throw new Error(`Error fetching access token for Supabase: ${error}`);
48+
}
49+
};
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import axios from 'axios';
2+
import qs from 'qs';
3+
import { DataObject, OAuthResponse } from '../../lib/types';
4+
5+
export const refresh = async ({ body }: DataObject): Promise<OAuthResponse> => {
6+
try {
7+
const {
8+
OAUTH_CLIENT_ID: client_id,
9+
OAUTH_CLIENT_SECRET: client_secret,
10+
OAUTH_REFRESH_TOKEN: refresh_token,
11+
OAUTH_REQUEST_PAYLOAD: { redirectUri: redirect_uri, tokenType },
12+
OAUTH_METADATA: { meta },
13+
} = body;
14+
15+
const requestBody = {
16+
grant_type: 'refresh_token',
17+
client_id,
18+
refresh_token,
19+
client_secret,
20+
redirect_uri,
21+
};
22+
23+
const response = await axios({
24+
url: 'https://api.supabase.com/v1/oauth/token',
25+
method: 'POST',
26+
headers: {
27+
'Content-Type': 'application/x-www-form-urlencoded',
28+
},
29+
data: qs.stringify(requestBody),
30+
});
31+
32+
const {
33+
access_token: accessToken,
34+
refresh_token: refreshToken,
35+
expires_in: expiresIn,
36+
} = response.data;
37+
38+
return {
39+
accessToken,
40+
refreshToken,
41+
expiresIn,
42+
tokenType,
43+
meta,
44+
};
45+
} catch (error) {
46+
throw new Error(`Error fetching access token for Supabase: ${error}`);
47+
}
48+
};

0 commit comments

Comments
 (0)