nuxt-alt / auth

An alternative module to @nuxtjs/auth
https://nuxt-alt-auth.vercel.app/
MIT License
96 stars 20 forks source link

Custom scheme implementation #4

Closed ManuelOchagavia closed 8 months ago

ManuelOchagavia commented 1 year ago

So we have some custom auth schemes within a nuxt2 app that we are trying to migrate from nuxt2 to nuxt3. We are currently struggling with the authentication since our auth was based on @nuxtjs/auth-next. I saw that this library was an alternative for it, but from the beginning i've found it difficult to find proper documentation to implement the migration to @nuxt-alt/auth. I would like to ask for few things:

Thanks for your help!

lowbits commented 1 year ago

Hey @ManuelOchagavia i think @Teranode did a nice job here and is probably currently not having the time to docu everything. What i can say while using his alternative, most of the time you can use the docu from auth.nuxtjs.org. When i stuck at some point i jump into the code and try to understand, what is happening.

For useAuth() you have the type declaration.

export declare class Auth {
    #private;
    ctx: NuxtApp;
    options: ModuleOptions;
    strategies: Record<string, Scheme>;
    $storage: Storage;
    $state: {
        strategy: string;
        user: Record<string, any>;
        loggedIn: boolean;
    };
    error?: Error;
    constructor(ctx: NuxtApp, options: ModuleOptions);
    getStrategy(throwException?: boolean): Scheme;
    get tokenStrategy(): TokenableScheme;
    get refreshStrategy(): RefreshableScheme;
    get strategy(): Scheme;
    get user(): Record<string, any> | null;
    get loggedIn(): boolean;
    get busy(): boolean;
    init(): Promise<void>;
    registerStrategy(name: string, strategy: Scheme): void;
    setStrategy(name: string): Promise<HTTPResponse | void>;
    mounted(...args: any[]): Promise<HTTPResponse | void>;
    loginWith(name: string, ...args: any[]): Promise<HTTPResponse | void>;
    login(...args: any[]): Promise<HTTPResponse | void>;
    fetchUser(...args: any[]): Promise<HTTPResponse | void>;
    logout(...args: any[]): Promise<void>;
    setUserToken(token: string | boolean, refreshToken?: string | boolean): Promise<HTTPResponse | void>;
    reset(...args: any[]): void;
    refreshTokens(): Promise<HTTPResponse | void>;
    check(...args: any[]): SchemeCheck;
    fetchUserOnce(...args: any[]): Promise<HTTPResponse | void>;
    setUser(user: any): void;
    request(endpoint: HTTPRequest, defaults?: HTTPRequest): Promise<HTTPResponse | void>;
    requestWith(endpoint?: HTTPRequest, defaults?: HTTPRequest): Promise<HTTPResponse | void>;
    wrapLogin(promise: Promise<HTTPResponse | void>): Promise<HTTPResponse | void>;
    onError(listener: ErrorListener): void;
    callOnError(error: Error, payload?: {}): void;
    /**
     *
     * @param name redirect name
     * @param route (default: false) Internal useRoute() (false) or manually specify
     * @param router (default: true) Whether to use nuxt redirect (true) or window redirect (false)
     *
     * @returns
     */
    redirect(name: string, route?: Route | false, router?: boolean): void;
    onRedirect(listener: RedirectListener): void;
    callOnRedirect(to: string, from: string): string;
    hasScope(scope: string): boolean;
}
steklopod commented 1 year ago

Duplicate of https://github.com/nuxt-alt/auth/issues/34

gbyesiltas commented 1 year ago

General documentation aside, is anyone able to help out with a RefreshScheme based CustomScheme implementation?

We did the following on Nuxt 2:

export default class CustomScheme extends RefreshScheme {
// ...
}

but while using this package, I can't quite figure out where I can properly import the RefreshScheme class from. Importing from "@nuxt-alt/auth/dist/runtime/schemes/refresh" looks like it works on Typescript, but it doesn't once the code compiles. It gives an error Missing "./dist/runtime/schemes/refresh" specifier in "@nuxt-alt/auth" package

Denoder commented 1 year ago

use #auth/runtime instead.

gbyesiltas commented 1 year ago

use #auth/runtime instead.

indeed, that works. thank you :)