Saltar al contenido

ejemplo de código sanctum vs passport vs jwt

No dudes en divulgar nuestra página y códigos en tus redes, necesitamos de tu ayuda para hacer crecer nuestra comunidad.

Ejemplo 1: laravel sanctum vs jwt

1.Passport: Passport provides a full OAuth2 server implementation for your 
  Laravel application in a matter of minutes. It is therefore necessary to have
  a brief knowledge of OAuth2.2.Sanctum: Sanctum it is a simple package to issue API tokens to your users
  without the complication of OAuth. Sanctum uses Laravel's built-in cookie
  based session authentication services.

In a small application use Sanctum. it's simple and easy

3.JWT:Auth (Authentication) is the process of identifying the user 
credentials. In web applications, authentication is managed by sessions which
take the input parameters such as email or username and password,for user
identification.If these parameters match, the user is said to be authenticated.

Ejemplo 2: laravel sanctum vs jwt

If using sanctum. The implementation will be asfollows:ForWEBFor web you dont need the token explicitly the sanctum/csrf-token handles 
everything for you. In case of web make sure you are allowing credentials forexample:

In Axios axios.defaults.withCredentials =true;

In JavaScript: xhr.withCredentials =true;.For Mobile authentication

For mobile authentication, you dont need to call sanctum/csrf-cookie API.

Please refer to the official doc section "Mobile Application Authentication".https://laravel.com/docs/7.x/sanctum#mobile-application-authentication.

General flow will be asfollows:1. Make a login APIand make sure you are not using auth: sanctum middleware
  with this.2. Call the login APIand validate user credentials andreturn a token on 
  success. You can refer following code:/**
     * Get a Token via given credentials.
     *
     * @returnIlluminateHttpJsonResponse
     */publicfunctionlogin()!Hash::check($credentials['password'],$user->password))returnresponse()->json(['message'=>'Unauthorized'],401);return$this->respondWithToken($user->createAccessToken(),["user"=>$user]);3. The user object has createToken() method to issue a token.4. Now usethis token with every request your making to the routes having 
  auth:sanctum middleware attached to itself.5. You need to add 'Authorization'=>'Bearer '.$access_token header in the
    request headers.

Si haces scroll puedes encontrar las interpretaciones de otros sys admins, tú asimismo puedes insertar el tuyo si dominas el tema.

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *