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

Laravel error 419 page expired
Publish date: 2021-04-10 | Comments: 2
Laravel clear cache files manually
Publish date: 2022-03-02 | Comments: 2
I want to make multiple foreign keys in Laravel, can I?
Publish date: 2022-01-27 | Comments: 5
How can i pass data from blade template to vue component?
Publish date: 2022-02-11 | Comments: 1
Laravel Network is unreachable stream_socket_client 587
Publish date: 2022-02-16 | Comments: 4