> For the complete documentation index, see [llms.txt](https://devworkshops.gitbook.io/masteringvuejs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://devworkshops.gitbook.io/masteringvuejs/consuming-apis/login.md).

# Login

In this section we're going to build a way to validate what the user enters and get the token back from the server.

In the **NorthwindService.js** file we're going to create an **AuthService** which will be used to talk the API and get the token back, we're also going to be able to retrieve user details.

{% code title="NorthwindTraders.js" %}

```javascript
export const AuthService = {
    currentUser: undefined,
    currentToken: undefined,
    isLoggedIn() {
        return !!this.currentToken
    },
    login(email, password) {
        return apiClient
            .post('/auth/login', { email, password })
            .then(response => {
                this.currentToken = response.data.access_token
                localStorage.setItem('token', this.currentToken)
                this.user()
                return response
            })
    },
    logout() {
        this.currentToken = null
        this.currentUser = null
        localStorage.removeItem('token')
    },
    token() {
        if (!this.currentToken) {
            this.currentToken = localStorage.getItem('token')
            if (this.currentToken) {
                this.user()
            }
        }
        return this.currentToken
    },
    user() {
        return apiClient.get('/user').then(response => {
            this.currentUser = response.data
        })
    }
}
```

{% endcode %}

We're now going to create a very basic login view for the user to enter the username and password and call the server to validate them

{% code title="Login.vue" %}

```markup
<template>
  <div>
    <h1>Login</h1>
    <form class="form">
      <div class="form-group row">
        <label class="col-form-label">Username</label>
        <input type="text" class="form-control" v-model="model.username">
      </div>
      <div class="form-group row">
        <label class="col-form-label">Password</label>
        <input type="password" class="form-control" v-model="model.password">
      </div>
      <div class="row">
        <button class="btn btn-primary" @click.prevent="login()">Login</button>
      </div>
    </form>
  </div>
</template>

<script>
import { AuthService } from '@/services/NorthwindService.js'
export default {
    data() {
        return {
            model: {}
        }
    },
    methods: {
        login() {
            AuthService.login(this.model.username, this.model.password)
                .then(() => {
                    this.$router.push('/suppliers')
                }
            )
        }
    }
}
</script>
```

{% endcode %}

Now that we have the token, it's time to inject to every single API call by updating the interceptors.

{% code title="NorthwindService.js" %}

```javascript
...
apiClient.interceptors.request.use(config => {
    ...
    if (AuthService.token()) {
        config.headers.authorization = 'Bearer ' + AuthService.token()
    }
    return config
})
...
```

{% endcode %}

We're also going to use the interceptor to redirect users to an unauthenticated view if we get back a HTTP response code of 401.

{% code title="Northwind.js" %}

```javascript
...
import router from '../router.js'
...
apiClient.interceptors.response.use(
    config => {
        NProgress.done()
        return config
    },
    err => {
        NProgress.done()
        if (err.response.status == 401) router.push('/unauthorized')
        throw err
    }
)
...
```

{% endcode %}

We need to create the **unauthorized** route and component. Let's create an **Unauthorized.vue** file and include the content below, also update the **router.js** with both login and unauthorized routes

{% code title="Unauthorized.vue" %}

```markup
<template>
    <div>
        <h1>Oops!!! You're apparently not authorized.</h1>
    </div>
</template>
```

{% endcode %}

{% code title="router.js" %}

```javascript
import Unauthorized from './views/Unauthorized.vue'
import Login from './views/Login.vue'
...
{
    path: '/login',
    component: Login
},
{
    path: '/unauthorized',
    component: Unauthorized
},
...
```

{% endcode %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://devworkshops.gitbook.io/masteringvuejs/consuming-apis/login.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
