nuxt-auth-utils
nuxt-auth-utils copied to clipboard
How can use Laravel passport
I need a simple example on how to use Laravel Passport :pray:
Hi, have same question
same question
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" },
}
}