1 | import { UnavailabilityError } from '@unimodules/core';
|
2 | import ExpoGoogleSignIn from './ExpoGoogleSignIn';
|
3 | import GoogleAuthentication from './GoogleAuthentication';
|
4 | import GoogleIdentity from './GoogleIdentity';
|
5 | class GoogleUser extends GoogleIdentity {
|
6 | constructor(options) {
|
7 | super(options);
|
8 | this.clearCache = async () => {
|
9 | if (!ExpoGoogleSignIn.clearCacheAsync) {
|
10 | return;
|
11 | }
|
12 | if (!this.auth || !this.auth.accessToken) {
|
13 | throw new Error('GoogleSignIn: GoogleUser.clearCache(): Invalid accessToken');
|
14 | }
|
15 | return await ExpoGoogleSignIn.clearCacheAsync({ token: this.auth.accessToken });
|
16 | };
|
17 | this.getHeaders = () => {
|
18 | if (!this.auth || !this.auth.accessToken || !this.auth.accessToken.length) {
|
19 | throw new Error('GoogleSignIn: GoogleUser.getHeaders(): Invalid accessToken');
|
20 | }
|
21 | return {
|
22 | Authorization: `Bearer ${this.auth.accessToken}`,
|
23 | Accept: 'application/json',
|
24 | 'Content-Type': 'application/json',
|
25 | };
|
26 | };
|
27 | this.refreshAuth = async () => {
|
28 | if (!ExpoGoogleSignIn.getTokensAsync) {
|
29 | throw new UnavailabilityError('GoogleSignIn', 'getTokensAsync');
|
30 | }
|
31 | const response = await ExpoGoogleSignIn.getTokensAsync(false);
|
32 | if (response.idToken == null && this.auth) {
|
33 | response.idToken = this.auth.idToken;
|
34 | }
|
35 | if (!this.auth) {
|
36 | this.auth = new GoogleAuthentication(response);
|
37 | }
|
38 | else {
|
39 | this.auth.idToken = response.idToken;
|
40 | this.auth.accessToken = response.accessToken;
|
41 | }
|
42 | return this.auth;
|
43 | };
|
44 | const { auth, scopes, hostedDomain, serverAuthCode } = options;
|
45 | this.auth = auth;
|
46 | this.scopes = scopes;
|
47 | this.hostedDomain = hostedDomain;
|
48 | this.serverAuthCode = serverAuthCode;
|
49 | }
|
50 | equals(other) {
|
51 | if (!super.equals(other) || !(other instanceof GoogleUser)) {
|
52 | return false;
|
53 | }
|
54 | if (this.auth != null) {
|
55 | return (this.auth.equals(other.auth) &&
|
56 | this.scopes === other.scopes &&
|
57 | this.hostedDomain === other.hostedDomain &&
|
58 | this.serverAuthCode === other.serverAuthCode);
|
59 | }
|
60 | else {
|
61 | return other.auth == null;
|
62 | }
|
63 | }
|
64 | toJSON() {
|
65 | let auth = this.auth;
|
66 | if (this.auth && this.auth.toJSON) {
|
67 | auth = this.auth.toJSON();
|
68 | }
|
69 | return {
|
70 | ...super.toJSON(),
|
71 | auth,
|
72 | scopes: this.scopes,
|
73 | hostedDomain: this.hostedDomain,
|
74 | serverAuthCode: this.serverAuthCode,
|
75 | };
|
76 | }
|
77 | }
|
78 | export default GoogleUser;
|
79 |
|
\ | No newline at end of file |