Saltar al contenido

use jwt y sanctum juntos en el ejemplo de código de api de laravel

No dejes de divulgar nuestros tutoriales y códigos con tus amigos, danos de tu ayuda para ampliar esta 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()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.

Calificaciones y reseñas

Nos encantaría que puedieras dar visibilidad a este enunciado si te ayudó.

¡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 *