Nuxt Auth - Sanctum

November 11th, 2020

To do this, we'll need to use the built in cookie scheme. The endpoints can be set to whichever routes you have available on your API, but the token information will need to be specified. We need to tell Nuxt to use the token property as the Authorization Bearer.

1auth: {
2 cookie: {
3 options: {
4 maxAge: 43200 // 12 hours
5 }
6 },
7 redirect: {
8 login: '/login',
9 logout: '/',
10 callback: '/',
11 home: '/dashboard',
12 },
13 strategies: {
14 default: {
15 endpoints: {
16 csrf: { url: 'csrf-cookie' },
17 login: { url: 'login', method: 'post' },
18 logout: { url: 'logout', method: 'delete' },
19 user: { url: 'auth', method: 'get' },
20 },
21 scheme: 'cookie',
22 token: {
23 name: 'Authorization',
24 global: true,
25 property: 'token',
26 required: 'true,
27 type: 'Bearer',
28 }
29 },
30 }
31}

The login method of our API needs to return json containing the plain text token.

1[login validation]
2 
3$token = $user->createToken($name);
4 
5return response()->json([
6 'token' => $token->plainTextToken,
7]);

And done! Users of your application will now be authenticated using personal access tokens from Sanctum.