user image

Joseph Morgan
Published in : 2022-07-16

Increase Sanctum token length in Laravel x9

Laravel

How to Increase Sanctum token length in Laravel 9?
this is my login function which generate the token 

<?php if(Auth::attempt(['email' => request('email') , 'password' => request('password')])){ $token = Auth::user()->createToken('authentication')->plainTextToken; $lastLogin = User::find(Auth::user()->id); $lastLogin->last_login = Carbon::now()->format('Y-m-d\TH:i:s.vP'); $lastLogin->save(); return response()->json([ "user" => Auth::user(), "token" => $token ], 200); } else { return response()->json(['Error, Wrong email or password'], 400); } ?>

and the token which generated by this function is too small 

5|FHtT0lg8w3AxJvIGYhxVf2PBYuGjgw3xALYDqkr8

How can I generate it like the Regular Bearer token? Any solution please

 

Comments

Mohamed Atef Date : 2022-07-16

Best answers

51

Best answers

51

Yes i believe you can do this, using this function in your User module 

<?php public function createToken(string $name, array $abilities = ['*']){ $token = $this->tokens()->create([ 'name' => $name, 'token' => hash('sha256', $plainTextToken = Str::random(200)), 'abilities' => $abilities, ]); return new NewAccessToken($token, $token->getKey().'|'.$plainTextToken); }

and don't forget to use 

use Illuminate\Support\Str;use Laravel\Sanctum\NewAccessToken;

at the top of your Module

Joseph Morgan Date : 2022-07-16

Thank you, I will try this function

Leave a comment

Join us

Join our community and get the chance to solve your code issues & share your opinion with us

Sign up Now

Related posts

How can I validate recaptcha in Livewire class ??
Publish date: 2022-02-23 | Comments: 2
How to pass valiables to 404 error page in laravel?
Publish date: 2022-02-21 | Comments: 2
Laravel install passport have access denied error
Publish date: 2022-02-23 | Comments: 4
Undefined Variable problem on Laravel 9.x
Publish date: 2022-03-06 | Comments: 3