Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Vue.js ❤️ JWT

Vue.js ❤️ JWT

JWT seems to be the new black in the authentication world. It is also a perfect fit when we need to add security in the client side. I will make an introduction to JWT, share my experience using it to "protect" SPA and show how easy is to implement it in your Vue.js app.

Ignacio Anaya

September 19, 2017
Tweet

More Decks by Ignacio Anaya

Other Decks in Programming

Transcript

  1. Ignacio Anaya @ianaya89 > Full Stack Developer, Tech Trainer &

    Speaker > @Auth0 Ambassador > @Vuenos_Aires Organizer { Vue.js ❤ JWT } - @ianaya89 2
  2. ! Why JWT? 4 Standard 4 Secure 4 Self Contained

    4 JSON ! { Vue.js ❤ JWT } - @ianaya89 5
  3. JWT ! Header { "alg": "HS256", "typ": "JWT" } {

    Vue.js ❤ JWT } - @ianaya89 7
  4. JWT ! Payload { "sub": "1234567890", "name": "John Doe", "admin":

    true, "iss": "https://api.com" } { Vue.js ❤ JWT } - @ianaya89 8
  5. JWT ! Use Cases 4 SPA's 4 Serverless 4 API's

    4 Mobile 4 IoT { Vue.js ❤ JWT } - @ianaya89 10
  6. ! What we need? 4 Auth Api 4 vue-router 4

    trae (http library) 4 localStorage { Vue.js ❤ JWT } - @ianaya89 12
  7. ! Auth API 4 Send credentials & get JWT 4

    Validate JWT 4 node, express, jsonwebtoken, express-jwt { Vue.js ❤ JWT } - @ianaya89 14
  8. ! vue-router 4 SPA's 4 1 route = 1 component

    { Vue.js ❤ JWT } - @ianaya89 15
  9. ! vue-router Routes Meta const router = new Router({ routes:

    [{ path: '/', component: Home }, { path: '/login', component: Login, meta: { isPublic: true } }] }) { Vue.js ❤ JWT } - @ianaya89 16
  10. ! vue-router Guardian Navs router.beforeEach((to, from, next) => { //

    resolve if user is authenticated... if (!to.meta.isPublic && !isAuthenticated()) { return next('login') } next() }) { Vue.js ❤ JWT } - @ianaya89 17
  11. ! trae 4 HTTP Library 4 Fetch Wrapper with steroids

    4 Similar axios 4 Promised Based { Vue.js ❤ JWT } - @ianaya89 18
  12. ! trae Example import trae from 'trae' trae.baseUrl('http://api:com') trae.get('/', {})

    .then(res => console.log(res.data)) .catch(err => console.log('[ERROR]', err)) { Vue.js ❤ JWT } - @ianaya89 19
  13. localStorage trae.post('/auth', { email, password }) .then(res => { localStorage.token

    = res.data localStorage.user = atob(res.data.split('.')[1]) }) .catch(err => console.log('[ERROR]', err)) { Vue.js ❤ JWT } - @ianaya89 21
  14. More! 4 jwt.io 4 Auth0 4 jwt-handbook 4 trae {

    Vue.js ❤ JWT } - @ianaya89 24