0.1.7 • Published 5 years ago

@studytweaks/vuejs-user v0.1.7

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

Vuejs user

Installation

via npm

npm install @studytweaks/vuejs-user

via yarn

yarn add @studytweaks/vuejs-user

Usage

Provided components are built on Buefy so install buefy first from there official site

For Font Awesome icons, add following to <head> :

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

Install Vuejs user module as follows:

# main.js

import userPlugin from '@studytweaks/vuejs-user'

import Profile from './models/Profile'
import User from './models/User'

let apiRoutes = {
  login: '/login',
  logout: '/logout',
  profile: '/profile',
  refresh: '/login/refresh'
}

let routes = {
  login: 'login',
  logout: 'login', // route to go after logout
  profile: 'profile'
}

// key is credential param used by user plugin, value is param needed by backend
let credentialsParamMapper = {
  username: 'email'
}

let profileModel = new Profile()
let userModel = new User()

Vue.use(
  userPlugin,
  {
    apiRoutes,
    axios,
    credentialsParamMapper,
    profileModel,
    redirectRoute: 'login',
    router,
    routes,
    store,
    userModel,
    vueAuthenticateOptions,
  }
)

Options

OptionDescriptionTypeDefault Value
apiRoutesAPI endpoints related to user authenticationObject
axiosThe instance of axios used by appObject
credentialsParamMapperOverridden credential keys to API keys mapperObject
loginRouteNameThe name of the login routeString'login'
profileModelThe stub instance of profile modelObjectStudytweaksProfile object
redirectAfterLogoutThe name of the route to redirect to after user logs outString'home'
routerThe registered router instanceObject
storeThe Vuex store to useObject
userModelThe stub instance of user modelObjectStudytweaksUser object
vueAuthenticateOptionsSocial login provider optionsObject

Note: routes option can have logout key whose value does not refer route of logout. It refers the route to go after logout.

Route meta fields

This plugin uses following meta fields in beforeEach guard of router instance. These can be specified when defining a route in the app

Meta field keyDescriptionTypeValue
redirectOnExpireRedirect to redirectRoute when both access token and refresh token gets expiredBooleantrue
requiresAuthMake route accessible to only authenticated user. Redirects to redirectRoute when user is not logged in.Booleantrue

Social login

For social login, install and setup vue-authenticate in your app as follows:

# main.js

let vueAuthenticateOptions = {
  baseUrl: process.env.API_BASE_URL, // API domain

  providers: {
    facebook: {
      clientId: process.env.FACEBOOK_CLIENT_ID,
      redirectUri: window.location.origin + '/'
    },
    github: {
      clientId: process.env.GITHUB_CLIENT_ID,
      redirectUri: window.location.origin
    },
    google: {
      clientId: process.env.GOOGLE_CLIENT_ID,
      redirectUri: window.location.origin
    },
    linkedin: {
      clientId: process.env.LINKEDIN_CLIENT_ID,
      redirectUri: window.location.origin
    }
  }
}

Vue.use(User, { store, router, redirectRoute: 'login', userEndpoints, vueAuthenticateOptions, axios })

Configure client ID of each platform that you want to enable on config/{environment}.env.js of your app.

Then you can render login component with various social login links by passing respective prop as follows:

<studytweaks-login facebook github google linkedin></studytweaks-login>

For login form on modal dialog

When mounting component if refresh token is expired(with Vue.$studytweaksAuth.isRefreshExpired()) or on axios request if error occurs with 401 Unauthorized status, you can authenticate using studytweaks-login component as modal body inside your modal.

You can set no-redirect prop to remain on current page after logging through modal dialog. You can listen to login:success event and handle closing modal dialog, re-requesting endpoint etc. on your event handler.

<studytweaks-login @login:success="hideLoginModal" no-redirect></studytweaks-login>

Component Reference

<studytweaks-login>

Properties

PropertyDescriptionTypeDefault Value
facebookFacebook login buttonBooleanfalse
githubGithub login buttonBooleanfalse
googleGoogle login buttonBooleanfalse
linkedinLinkedin login buttonBooleanfalse
redirectRoute to redirect to after login. false for no redirect.'BooleanStringObject''home'
titleTitle to show as headingStringtranslated user.login.title key

Events

EventDescription
login:erroremits after error while logging in
login:startemits before invoking login
login:successemits after successful login

<studytweaks-profile>

Properties

PropertyDescriptionTypeDefault Value
update-urlAPI route to update current user profileString

Events

EventDescription
before-updateemits before invoking update
unauthorized-erroremits if response status is 401 while refreshing token
update-erroremits after error while updating profile
update-successemits after successful profile update

<studytweaks-user-menu>

Properties

PropertyDescriptionTypeDefault Value
dividerShow dropdown dividerBooleantrue
loginLogin routeObject, String'login'
profileProfile routeObject, String
rightRight align dowpdown menuBooleanfalse
signupSignup routeObject, String'signup'
welcomeContent to show on menu buttonStringtranslated user.menu.welcome key
0.1.7

5 years ago

0.1.6

5 years ago

0.1.5

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago