# All files / atlas/src/stores AuthStore.ts

29.41% Statements 5/17
0% Branches 0/6
25% Functions 1/4
29.41% Lines 5/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90

    7x

    7x

    7x

    7x

    7ximport { action, makeObservable, observable } from "mobx";
    import * as SecureStore from 'expo-secure-store';

    /**
     * Represents OIDC ID token.
     */
    export interface IdToken {
        sub: string
    }

    /**
     * A mobx store responsible for holding the current access token, refresh token and userId in memory and in Expo's [SecureStore]{@link https://docs.expo.dev/versions/latest/sdk/securestore/}.
     * Used by {@link Atlas}
     * @category Stores
     */
    class AuthStore {
        /**
         * A string representing the OAuth2 access token issued to the user when they authenticate. It is sent in the Authorization header of all subsequent XHR requests in order to use authorized API endpoints.
         * For more information on OAuth2 and OpenIdConnect, visit {@link https://openid.net/connect/} and {@link https://oauth.net/2/}.
         * @memberOf AuthStore
         */
        accessToken?: string | null = null;
        /**
         * A string representing the OAuth2 refresh token issued to the user when they authenticate. It is sent to the identity provider to retrieve a new access token when the previous access token expires.
         * @memberOf AuthStore
         */
        refreshToken?: string | null = null;
        /**
         * A string representing the OAuth2 refresh token issued to the user when they authenticate. It is used to retrieve the users information from the API and to link objects created by the user to the user.
         */
        userId?: string = undefined;

        /**
         * Constructor. Uses mobx's [makeObservable]{@linkcode https://mobx.js.org/observable-state.html} to register actions and observable elements.
         */
        constructor() {
            makeObservable(this, {
                accessToken: observable,
                refreshToken: observable,
                userId: observable,
                setAccessTokenAsync: action,
                setRefreshTokenAsync: action,
                setIdAsync: action,
            });
        }

        /**
         * Uses the given access token value to set {@linkcode accessToken} and the 'access' element in the secure store.
         */
        async setAccessTokenAsync(tokenValue: string | null) {
            this.accessToken = tokenValue;
            if (this.accessToken) {
                await SecureStore.setItemAsync('access', this.accessToken);
            }
            else
            {
                await SecureStore.deleteItemAsync('access');
            }
        }

        /**
         * Uses the given refresh token value to set {@linkcode refreshToken} and the 'refresh' element in the secure store.
         */
        async setRefreshTokenAsync(tokenValue: string | null) {
            this.refreshToken = tokenValue;
            if (this.accessToken) {
                await SecureStore.setItemAsync('refresh', this.accessToken);
            }
            else
            {
                await SecureStore.deleteItemAsync('refresh');
            }
        }

        /**
         * Uses the given user id value to set {@linkcode userId} and the 'id' element in the secure store.
         */
        async setIdAsync(id?: string) {
            this.userId = id;
            if (this.userId) {
                await SecureStore.setItemAsync('id', this.userId);
            }
            else
            {
                await SecureStore.deleteItemAsync('id');
            }
        }
    }

    export const authStore = new AuthStore();