nuxt-auth-utils icon indicating copy to clipboard operation
nuxt-auth-utils copied to clipboard

How can use Laravel passport

Open adil-chbada opened this issue 1 year ago • 9 comments

I need a simple example on how to use Laravel Passport :pray:

adil-chbada avatar Feb 24 '24 18:02 adil-chbada

Hi, have same question

frontender-ua avatar Apr 06 '24 13:04 frontender-ua

same question

jjjrmy avatar Aug 06 '24 01:08 jjjrmy

import type { OAuthConfig, OAuthUserConfig } from '@auth/core/providers'

export interface PassportProfile {
    id: number
    name: string
    email: string | null
    created_at: string
    updated_at: string
}

export default function Passport(
    config: OAuthUserConfig<PassportProfile> & {
        baseUrl: string
    }
): OAuthConfig<PassportProfile> {
    return {
        id: "passport",
        name: "Laravel Passport",
        type: "oauth",

        clientId: config.clientId,
        clientSecret: config.clientSecret,

        authorization: {
            url: `${config.baseUrl}/oauth/authorize`,
            params: {
                scope: "",
            },
        },
        token: {
            url: `${config.baseUrl}/oauth/token`,
            async request({ params, provider }) {
                const res = await fetch(provider.token.url, {
                    method: "POST",
                    headers: {
                        "Cache-Control": "no-cache",
                        "Content-Type": "application/x-www-form-urlencoded",
                    },
                    body: new URLSearchParams({
                        client_key: provider.clientId,
                        code: params.code,
                        grant_type: "authorization_code",
                        redirect_uri: provider.callbackUrl,
                    }),
                }).then((res) => res.json());

                return {
                    tokens: {
                        access_token: res.access_token,
                        expires_at: res.expires_in,
                        refresh_token: res.refresh_token,
                        token_type: res.token_type,
                    },
                };
            }
        },
        userinfo: {
            url: `${config.baseUrl}/api/user`,
            async request({ tokens, provider }) {
                return await fetch(provider.userinfo?.url, {
                    headers: { Authorization: `Bearer ${tokens.access_token}` },
                }).then(async (res) => await res.json());
            },
        },
        profile(profile) {
            return {
                id: profile.id.toString(),
                name: profile.name,
                email: profile.email,
            }
        },
        checks: ["state"],
        style: { bg: "#ff0000", text: "#fff", logo: "https://laravel.com/img/logomark.min.svg" },
    }
}

jjjrmy avatar Aug 10 '24 23:08 jjjrmy