go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/projects/nodes/static/_nextjs/src/api/session.ts (about)

     1  /**
     2   * Copyright (c) 2024 - Present. Will Charczuk. All rights reserved.
     3   * Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     4   */
     5  export interface AuthorizedSession {
     6    authorized: true
     7    userID: string
     8    baseURL?: string
     9    sessionID: string
    10    createdUTC?: Date
    11    expiresUTC?: Date
    12    userAgent?: string
    13    remoteAddr?: string
    14    locale?: string
    15    state?: SessionState
    16  }
    17  
    18  export interface SessionState {
    19    User?: User
    20  }
    21  
    22  export interface User {
    23    ID?: string;
    24    CreatedUTC?: Date;
    25    LastLoginUTC?: Date;
    26    LastSeenUTC?: Date;
    27    GivenName?: string;
    28    FamilyName?: string;
    29    PictureURL?: string;
    30    Locale?: string;
    31    Email?: string;
    32  }
    33  
    34  export interface NotAuthorizedSession {
    35    authorized: false;
    36  }
    37  export type Session = AuthorizedSession | NotAuthorizedSession
    38  
    39  export async function getSession(): Promise<Session> {
    40    const res = await fetch(`/api/v1/session`);
    41    const data = await res.json();
    42    if (!res?.ok) {
    43      return { authorized: false };
    44    }
    45    return { ...data, authorized: true } as AuthorizedSession
    46  };