|
| 1 | +import { __HOSTS } from "@base-sdk/core"; |
1 | 2 | import { |
2 | 3 | __auth_proxy, |
3 | 4 | ProxyAuthenticationMode, |
4 | | -} from "@base-sdk/auth-first-party"; |
5 | | -import { __HOSTS } from "@base-sdk/core"; |
| 5 | + AuthProxySessionStartRequest, |
| 6 | + AuthProxySessionStartResult, |
| 7 | +} from "@base-sdk-fp/auth"; |
| 8 | +import { client_id } from "plugin-app"; |
| 9 | +import { AuthStorage } from "./storage"; |
6 | 10 |
|
7 | 11 | const PROXY_AUTH_REQUEST_SECRET = |
8 | | - process.env.BRIDGED_FIRST_PARTY_PROXY_AUTH_REQUEST_TOTP_SECRET; |
| 12 | + process.env.GRIDA_FIRST_PARTY_PROXY_AUTH_REQUEST_TOTP_SECRET ?? |
| 13 | + process.env.NEXT_PUBLIC_GRIDA_FIRST_PARTY_PROXY_AUTH_REQUEST_TOTP_SECRET; |
| 14 | + |
| 15 | +function _make_request(): AuthProxySessionStartRequest { |
| 16 | + return { |
| 17 | + appId: "co.grida.assistant", |
| 18 | + clientId: client_id, |
| 19 | + mode: ProxyAuthenticationMode.long_polling, |
| 20 | + redirect_uri: "figma://", // TODO: change this scheme based on target platform. |
| 21 | + }; |
| 22 | +} |
| 23 | + |
| 24 | +export async function startAuthenticationSession(): Promise<AuthProxySessionStartResult> { |
| 25 | + return __auth_proxy.openProxyAuthSession( |
| 26 | + PROXY_AUTH_REQUEST_SECRET, |
| 27 | + _make_request() |
| 28 | + ); |
| 29 | +} |
| 30 | + |
| 31 | +export async function startAuthenticationWithSession( |
| 32 | + session: AuthProxySessionStartResult |
| 33 | +) { |
| 34 | + const result = await __auth_proxy.requesetProxyAuthWithSession( |
| 35 | + PROXY_AUTH_REQUEST_SECRET, |
| 36 | + session, |
| 37 | + _make_request() |
| 38 | + ); |
| 39 | + |
| 40 | + AuthStorage.save(result.access_token); |
| 41 | + // save result |
| 42 | + |
| 43 | + return result; |
| 44 | +} |
9 | 45 |
|
10 | 46 | export async function startAuthentication() { |
11 | | - const request = await __auth_proxy.requesetProxyAuth({ |
12 | | - mode: ProxyAuthenticationMode.ws, |
13 | | - secret: PROXY_AUTH_REQUEST_SECRET, |
14 | | - }); |
| 47 | + const session = await startAuthenticationSession(); |
| 48 | + return await startAuthenticationWithSession(session); |
| 49 | +} |
| 50 | + |
| 51 | +export async function isAuthenticated() { |
| 52 | + return (await AuthStorage.get())?.length > 1; // using 1 (same as != undefined.) |
| 53 | +} |
| 54 | + |
| 55 | +export async function checkAuthSession(session: string): Promise<boolean> { |
| 56 | + // TODO: |
| 57 | + const res = await __auth_proxy.checkProxyAuthResult( |
| 58 | + PROXY_AUTH_REQUEST_SECRET, |
| 59 | + session |
| 60 | + ); |
15 | 61 |
|
16 | | - // todo - build full url using method in base sdk |
17 | | - open(__HOSTS.INTERNAL_SECURE_ACCOUNTS_SERVICE_HOST); |
| 62 | + const success = res.success && res.access_token !== undefined; |
| 63 | + if (success) { |
| 64 | + AuthStorage.save(res.access_token); |
| 65 | + } |
| 66 | + return success; |
18 | 67 | } |
0 commit comments