Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can use Laravel passport #62

Open
adil-chbada opened this issue Feb 24, 2024 · 3 comments
Open

How can use Laravel passport #62

adil-chbada opened this issue Feb 24, 2024 · 3 comments
Labels
question Further information is requested

Comments

@adil-chbada
Copy link

I need a simple example on how to use Laravel Passport 🙏

@adil-chbada adil-chbada changed the title How can use Laravel passport 😅 How can use Laravel passport Feb 25, 2024
@atinux atinux added the question Further information is requested label Mar 19, 2024
@frontender-ua
Copy link

Hi, have same question

@jjjrmy
Copy link

jjjrmy commented Aug 6, 2024

same question

@jjjrmy
Copy link

jjjrmy commented Aug 10, 2024

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" },
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants