Permalink
Browse files

feat: initial migration to 1.0.0-alpha1

BREAKING CHANGE: New modules system is backward incompatible with nuxt-helpers style modules
  • Loading branch information...
pi0 committed May 26, 2017
1 parent 5619c1c commit 05c1b7a19e1f075e9dd538c11e25a8d93ed6fed7

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
@@ -10,7 +10,7 @@
<a href="https://donorbox.org/nuxt"><img src="https://img.shields.io/badge/Support%20us-donate-41B883.svg?style=flat-square" alt="Support us"></a>
</p>
## 🛑 Under heavy development, Please use [nuxt-helpers](https://github.com/fandogh/nuxt-modules) as an alternative for now.
## 🛑 Under development.
## Official Modules
- [axios](modules/axios)
@@ -25,14 +25,8 @@
- [vendor](modules/vendor)
# Modules Development Guide
Nuxt has powerful SDK which allows you write your own modules and share them with community.
Learn More: [Modules Development Guide](MODULES.md)
## Alpha Testing
Nuxt.js modules system is not released yet. So until then you can use a wrapper to preview nuxt modules.
Learn More: [Alpha Testing](ALPHA.md)
Nuxt has powerful SDK which allows you write your own npm modules and share them with community.
[Guide Coming Soon]
## License
[MIT License](LICENSE.md) - Nuxt.js 2017
@@ -1,21 +1,14 @@
## Auth Store
#### 💡 Usage
- Add `auth` module
# Auth Store (WIP)
## Setup
- Add `@nuxtjs/auth` dependency using yarn or npm to your project
- Add `@nuxtjs/auth` module to `nuxt.config.js`
```js
// store/auth.js
import AuthStore from '~/.nuxt-modules/auth';
const authStore = new AuthStore({ /*opts*/ });
// Your customizations
export default authStore;
```
modules: [
'@nuxtjs/auth'
]
````
**Options**
## Options
- **default_user** : Default fields for `state.auth.user`. (overrides using Object.assign when logged-in).
- **token_cookie** : Token cookie opts. (see [js-cookie docs](https://github.com/js-cookie/js-cookie) for more info)
@@ -1,10 +1,7 @@
const path = require('path')
module.exports.meta = {
name: 'nuxt-auth',
vendor: ['cookie', 'js-cookie'],
plugin: {
src: path.resolve(__dirname, 'store.js'),
copyOnly: true
}
module.exports = function nuxtAuth(options) {
// TODO: Add store: path.resolve(__dirname, 'store.js')
}
module.exports.meta = require('./package.json')
@@ -5,54 +5,50 @@
import Cookie from 'cookie'
import Cookies from 'js-cookie'
import {setToken, $get, $post, $delete} from './axios' // Axios is a peer plugin dependency
const inBrowser = typeof window !== 'undefined'
const SSR = global.__VUE_SSR_CONTEXT__
function AuthStore (opts) {
function AuthStore(opts) {
const self = this
opts = opts || {}
// ----------------------------------------
// Default State
// ----------------------------------------
this.defaultState = {
user: Object.assign({roles: [], scope: [], name: null}, opts.default_user),
loggedIn: false,
token: null
// ----------------------------------------
// State
// ----------------------------------------
this.state = () => {
return {
user: Object.assign({roles: [], scope: [], name: null}, opts.default_user),
loggedIn: false,
token: null
}
}
// ----------------------------------------
// State
// ----------------------------------------
this.state = Object.assign({}, self.defaultState)
// ----------------------------------------
// Getters
// ----------------------------------------
// ----------------------------------------
// Getters
// ----------------------------------------
this.getters = {}
// ----------------------------------------
// Mutations
// ----------------------------------------
// ----------------------------------------
// Mutations
// ----------------------------------------
this.mutations = {
setUser (state, user) {
// Fill user with defaults data
// Fill user with defaults data
state.user = Object.assign({}, self.defaultState.user, user)
// Set actual loggedIn status
// Set actual loggedIn status
state.loggedIn = Boolean(user)
},
setToken (state, token) {
state.token = token
// Setup axios
// Setup axios
setToken(token)
// Store token in cookies
// Store token in cookies
if (inBrowser) {
if (!token) {
return Cookies.remove('token', opts.tokenCookie)
@@ -63,13 +59,13 @@ function AuthStore (opts) {
}
// ----------------------------------------
// Actions
// ----------------------------------------
// ----------------------------------------
// Actions
// ----------------------------------------
this.actions = {
loadToken (ctx) {
// Try to extract token from cookies
// Try to extract token from cookies
const cookieStr = inBrowser ? document.cookie : SSR.req.headers.cookie
const cookies = Cookie.parse(cookieStr || '') || {}
const token = cookies.token
@@ -78,15 +74,15 @@ function AuthStore (opts) {
},
fetch (ctx) {
// Load user token
// Load user token
ctx.dispatch('loadToken')
// No token
// No token
if (!ctx.state.token) {
return
}
// Get user profile
// Get user profile
return $get('/auth/user').then(userData => {
ctx.commit('setUser', userData.user)
}).catch(() => {
@@ -96,7 +92,7 @@ function AuthStore (opts) {
login (ctx, {fields, endpoint = '/auth/login', session = false}) {
return $post(endpoint, fields).then(tokenData => {
// Session tokens
// Session tokens
if (session) {
opts.tokenCookie = null
}
@@ -106,18 +102,18 @@ function AuthStore (opts) {
},
logout (ctx, {endpoint = '/auth/logout', appendToken = false}) {
// Unload user profile
// Unload user profile
ctx.commit('setUser', null)
// Append token
// Append token
if (appendToken) endpoint = endpoint + `/${ctx.state.token}`
// Server side logout
// Server side logout
return $delete(endpoint).then(() => {
// Unset token
// Unset token
ctx.commit('setToken', null)
}).catch(() => {
// Unset token
// Unset token
ctx.commit('setToken', null)
})
}
@@ -1,37 +1,43 @@
## Axios
# Axios
This plugin is a wrapper around [axios](https://github.com/mzabriskie/axios). It tries to resolve and make easier lot's of ajax tasks specially with SSR.
So you can use **$get('profile')** instead of `(await Axios.get('http://server/api/profile')).data`.
- Uses optionally custom URL when executing requests in server-side.
- Handles all HTTP exceptions and prevents server side unhandled promise exceptions.
- Injects `$get`,`$post`,... into vue context instances so requests can be done out-of-the-box.
- Exposes `setToken` function so we can easily and globally set authentication tokens.
- Returns empty object if request fails.
- Throws *nuxt-friendly* exceptions if needed.
#### 💡 Usage
## Setup
- Add `@nuxtjs/axios` dependency using yarn or npm to your project
- Add `@nuxtjs/axios` module to `nuxt.config.js`:
```js
modules: [
'@nuxtjs/axios'
]
````
- Add `axios` module
## Usage
### Using inside `asyncData`
```js
import {$get} from '~/nuxt-modules/axios';
async data() {
let {profile} = await $get('profile');
return {profile}
async asyncData({app: {$axios}}) {
const {data} = await $axios.get('http://icanhazip.com')
return {
ip: data
}
}
```
Or In any other function: (This does not needs importing axios plugin)
### Using inside component methods
```js
mounted() {
let {profile} = await this.$get('profile');
return {profile}
async mounted() {
const {data} = await this.$get('http://icanhazip.com')
this.ip = data
}
```
**Customization**
## Customization
Customization can be done using shared environment variables.
Oops, something went wrong.

0 comments on commit 05c1b7a

Please sign in to comment.