Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: multi strategy #61

Merged
merged 42 commits into from
Feb 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
89bee9d
chore: update dependencies
Feb 6, 2018
82e8f79
refactor: internal refactor to simplify template names
Feb 6, 2018
6572f08
refactor: initial refactor for multi strategy support
Feb 7, 2018
cfc5645
fix eslint
Feb 7, 2018
f53be87
update tests
Feb 7, 2018
cd6cdda
Merge branch 'master' into multi-strategy
Feb 7, 2018
beb3121
fix(fetchUser): fetchUser should only be called when enabled (#60)
Feb 7, 2018
b6b94dd
more refactors for multi strategy
Feb 8, 2018
4f26234
:boom: feat: true working multi strategy
Feb 16, 2018
4623a5c
:shirt chore: update test and examples
Feb 16, 2018
f724fef
:books: prepare docs for a new website
Feb 16, 2018
91fbae5
apply e94b726e6edcae149d8d67985d05b4dabf989746
Feb 16, 2018
8f4e9a9
Merge branch 'master' into multi-strategy
pi0 Feb 16, 2018
70447de
docs: remove fetchUserOnLogin
Feb 16, 2018
49094ca
update docs
Feb 16, 2018
0764684
docs: add migration guide and changelog
Feb 16, 2018
1ea0918
fix SUMMARY.md
Feb 16, 2018
ce03d9d
update docs
Feb 16, 2018
b2c9280
update docs
Feb 16, 2018
20760e9
update docs
Feb 16, 2018
03a668c
docs
Feb 16, 2018
f3ab1e9
use symlink for CHANGELOG.md
Feb 16, 2018
c9c9af3
update book.json
Feb 16, 2018
7316848
:book: docs: improve reading flow
Feb 16, 2018
34aa7c9
:book: docs: refactor usage api
Feb 16, 2018
814ba41
refactor docs
Feb 16, 2018
277a4c0
docs: typos
Feb 16, 2018
0238fc8
links
Feb 16, 2018
a6ceef4
update readme.md
Feb 16, 2018
ec4ee25
refactor: use class constructor for schemes
Feb 16, 2018
c38a1e4
feat: add auth0-js scheme
Feb 16, 2018
4dbe802
misc: add setUser helper
Feb 16, 2018
340ab5d
update local.md
Feb 16, 2018
a51ee6d
docs: add auth0 docs
Feb 16, 2018
4caa59a
update auth0.md
Feb 16, 2018
ddeaa14
update docs
Feb 16, 2018
2e58c80
update readme
Feb 16, 2018
63b54a8
update readme
Feb 16, 2018
3788827
:book auth.nuxtjs.org
Feb 19, 2018
61e27ff
update readme
Feb 19, 2018
f26bb20
update readme
Feb 19, 2018
2aed448
feat: loginWith function
Feb 19, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
259 changes: 7 additions & 252 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<h1 align="center">🔑 Auth</h1>
# 🔑 Auth Module

<p align="center">Authentication module for Nuxt.js</p>
> Authentication module for Nuxt.js

<p align="center">
<a href="https://david-dm.org/nuxt-community/auth-module">
Expand All @@ -24,260 +24,15 @@
</a>
</p>

[📖 **Release Notes**](./CHANGELOG.md)
📖 [**Read Documentation**](https://auth.nuxtjs.org)

If you are coming from an older release please be sure to read [Migration Guide](https://github.com/nuxt-community/auth-module/wiki/Migration-guide).

<h2 align="center">Setup</h2>

Install with yarn:

```bash
yarn add @nuxtjs/auth @nuxtjs/axios
```

Install with npm:
**Running demo for development:**

```bash
npm install @nuxtjs/auth @nuxtjs/axios
```

Edit `nuxt.config.js`:

```js
{
modules: [
'@nuxtjs/axios',
'@nuxtjs/auth'
],

auth: {
// Options
}
```

See [Options](#options) section for all available options

<h2 align="center">Usage</h2>

Do a password based login:

```js
this.$auth.login({
data: {
username: 'your_username',
password: 'your_password'
}
})
```

`user` object:

```js
// Access using $auth (reactive)
this.$auth.state.user

// Access using $store (reactive)
this.$store.state.auth.user

// Refetch user
this.$auth.fetchUser()
```

`loggedIn` status:

```js
// Access using $auth (reactive)
this.$auth.state.loggedIn

// Access using $store (reactive)
this.$store.state.auth.loggedIn

// Do logout
this.$auth.logout()
```

Check if user has a speficic scope:

```js
// Returns is a computed boolean
this.$auth.hasScope('admin')
```

Auth token:

```js
// Access token (reactive)
this.$auth.token

// Update token
this.$auth.setToken('123')
```

Listen for auth errors: (`plugins/auth.js`)

```js
export default function({ $auth }) {
$auth.onError((error, name, endpoint) => {
console.error(name, error)
})
}
```

Working with low level state: (Not recommended)

```js
// Store
this.$auth.setState(key, val)
this.$auth.getState(key)

// Watch state changes
this.$auth.watchState('loggedIn', newValue => { })

// Cookie
this.$auth.setCookie(key, val, options)
this.$auth.getCookie(key)

// LocalStorage
this.$auth.setLocalstorage(key, val, options)
this.$auth.getLocalstorage(key)
```

<h2 align="center">Auth Middleware</h2>

You can enable `auth` middleware either globally or per route.
When this middleware is enabled on a route and `loggedIn` is `false` user will be redirected to `redirect.login` route. (`/login` by default)

Setting per route:

```js
export default {
middleware: 'auth'
}
```

Globally setting in `nuxt.config.js`:

```js
router: {
middleware: ['auth']
}
```

In case of global usage, You can set `auth` option to `false` in a specific component and the middleware will ignore that route.

```js
export default {
auth: false
}
```

<h2 align="center">Options</h2>

See [defaults.js](lib/defaults.js) for defaults.

### `endpoints`

Default:

```js
endpoints: {
login: { url: '/api/auth/login', method: 'post', propertyName: 'token' },
logout: { url: '/api/auth/logout', method: 'post' },
user: { url: '/api/auth/user', method: 'get', propertyName: 'user' }
}
```

Endpoints used to make requests using axios. They are basically extending Axios [Request Config](https://github.com/axios/axios#request-config).

`propertyName` can be used to specify which field of the response to be used for value. It can be `undefined` to directly use API response or being more complicated like `auth.user`.

To disable each endpoint, simply set it's value to `false`.

### `redirect`

Default:

```js
redirect: {
login: '/login',
home: '/'
},
```

Redirect paths to redirect user after login and logout. Each can be disabled by setting to `false`.

### `token`

Default:

```js
token: {
type: 'Bearer',
name: 'token'.
}
```

* **type** - Authotization header type to be used in axios requests.
* **name** - Token name to be stored in Browser localStorage. It can be disabled by setting to `false`.

### `cookie`

Default:

```js
cookie: {
name: 'token',
options: {
path: '/'
}
}
$ yarn install
$ yarn dev
```

Using cookies is **required** for SSR requests to work with JWT tokens.

It can be disabled by setting `cookie` to `false`.

* **name** - Cookie name.
* **options** - Cookie options.
* `options.expires` can be used to speficy cookie lifetime in days. Default is session only.

### `fetchUserOnLogin`

* Default: `true`

If enabled, user will be auto fetched after login.

### `resetOnError`

* Default: `false`

If enabled, user will be automatically logged out if any error happens. (For example when token expired)

### `rewriteRedirects`

* Default: `true`

If enabled, user will redirect back to the original guarded route instead of `redirect.home`.

### `watchLoggedIn`

* Default: `true`

If enabled, user will automatically redirected to `redirect.home` after login/logout.

### `namespace`

* Default: `auth`

Vuex store namespace for keeping state.

### `scopeKey`

* Default: `scope`

`user` object proprty used for scope checkings (`hasScope`). Can be either an array or a object.

## License
## 📑 License

[MIT License](./LICENSE) - Copyright (c) Nuxt Community
27 changes: 27 additions & 0 deletions book.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"title": "Nuxt Auth Module",
"gitbook": ">3.0.0",
"root": "./docs",
"plugins": [
"edit-link",
"prism",
"theme-vuejs-2",
"-fontsettings",
"github"
],
"pluginsConfig": {
"edit-link": {
"base": "https://github.com/nuxt-community/auth-module/tree/master/docs",
"label": "Edit This Page"
},
"github": {
"url": "https://github.com/nuxt-community/auth-module"
}
},
"links": {
"sharing": {
"facebook": false,
"twitter": false
}
}
}
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
7 changes: 7 additions & 0 deletions docs/GLOSSARY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Scheme

An *unconfigured* authentication provider implementation.

## Strategy

An *configured* Schemes.
13 changes: 13 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Auth Module

> Authentication module for Nuxt.js.

## Links

* [Github](https://github.com/nuxt-community/auth-module)
* [Release Notes](./CHANGELOG.md)
* [Migration Guide](migration.md)
* [Examples](https://github.com/nuxt-community/auth-module/tree/master/examples)
* [Demo](https://nuxt-auth.herokuapp.com)

> 👉 To get started head to [Setup](setup.md) section.
14 changes: 14 additions & 0 deletions docs/SUMMARY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Summary

* [Introduction](README.md)
* [Setup](setup.md)
* [Middleware](middleware.md)
* [Strategies](strategies/README.md)
* [Local](strategies/local.md)
* [Auth0](strategies/auth0.md)
* [API](api/README.md)
* [Properties](api/properties.md)
* [Methods](api/methods.md)
* [Options](options.md)
* [Migration Guide](migration.md)
* [Changelog](CHANGELOG.md)
7 changes: 7 additions & 0 deletions docs/api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Usage API

* [Reactive Properties](properties.md)
* [Auth Methods](methods.md)



Loading