The idea behind OAuth is to use tokens (instead of username and password) for authentication. We do this because of three reasons:
- Tokens are more secure than username and password combinations since they more advanced hashing algorithms.
- Tokens can contain more information than username and password, which can be used for authorization. (More on this in a later lesson on scope).
- Tokens allow users to login with different identity providers (like Github, Twitter, etc) because these providers can send tokens to other applications without compromising a user’s password.
The token used for authenticating a user (or app) is called an access token.
Authenticating with an access token
When you have an access token, you can authenticate yourself by passing the token into a Authorization header. Make sure the Authorization header begins with Bearer.
fetch('link-to-resource', { method: 'post, headers: { Authorization: `Bearer ${token}` }})zlFetch makes things easier by writing the Authorization header for you. You only have to pass the token into an auth property and zlFetch will do the rest.
zlFetch.post('link-to-resource', { auth: token,})Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson