Giter Site home page Giter Site logo

amazon-cognito-vuex-module's Introduction

Amazon Cognito Vuex Module

Vuex module for Amazon Cognito

Easily link your Vuex application to an Amazon Cognito User Pool.

Notes

  • Federated Identities are not supported (pull request welcome).
  • Email is used instead of username.
  • API naming parallels AWS SDK naming.
  • Module size: +-300KB, optimization pull requests are welcome.
  • All actions are wrapped in Promises.

Links

Installation

npm install amazon-cognito-vuex-module --save
import Vue from 'vue';
import Vuex from 'vuex';
import App from './App.vue';
import AmazonCognitoVuexModule from 'amazon-cognito-vuex-module';

Vue.use(Vuex);

const store = new Vuex.Store({
  modules: {
    cognito: new AmazonCognitoVuexModule({
      region: '<region>',
      userPoolId: '<user pool id>',
      clientId: '<client id>'
    })
  }
});

new Vue({
  el: '#app',
  render: h => h(App),
  store
});

Axios interceptor example

// Add authentication token to each request
axios.interceptors.request.use(async config => {
  const response = await store.dispatch('getUserSession');
  if (response && response.accessToken && response.accessToken.jwtToken) {
    config.headers.AccessToken = response.accessToken.jwtToken;
  }
  return config;
});

API

State

Whether user authentication is currently in progress

$store.state.cognito.authenticating

The authenticated user (null if none)

$store.state.cognito.authenticated

Actions

Check whether a user is currently authenticated, if so: update state

$store.dispatch('checkAuthentication')

Authenticate user and establish session

$store.dispatch('authenticateUser', {email: '[email protected]', password: 'foobar'})

Fetch user session

$store.dispatch('getUserSession')

Fetch attributes of the authenticated user

$store.dispatch('getUserAttributes')

Change password of the currently authenticated user (given the current and new passwords)

$store.dispatch('changePassword', {currentPassword: 'Hello', newPassword: 'World!'})

Initiate lost password procedure (send verification code to user)

$store.dispatch('forgotPassword', {email: '[email protected]'})

Confirm a new password for a given email, verification code and new password

$store.dispatch('confirmPassword', {email: '[email protected]', verificationCode: 1337, newPassword: 'dontforgetme'})

Sign up a new user for a given email and password (then sends registration verification code)

$store.dispatch('signUp', {email: '[email protected]', password: 'dontforgetme'})

Confirm a registration for a given email and registration verification code

$store.dispatch('confirmRegistration', {email: '[email protected]', verificationCode: 1337})

Re-send the registration verification code

$store.dispatch('resendConfirmationCode', {email: '[email protected]'})

Sign out user

$store.dispatch('signOut')

amazon-cognito-vuex-module's People

Contributors

botre avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.