GithubHelp home page GithubHelp logo

kunkgg / authentication_course Goto Github PK

View Code? Open in Web Editor NEW

This project forked from code-pop/authentication_course

0.0 0.0 0.0 53 KB

Repo for Vue Mastery's Token-Based Authentication course

Home Page: https://www.vuemastery.com/courses/token-based-authentication/intro-to-authentication

JavaScript 27.87% CSS 15.27% HTML 1.52% Vue 55.33%

authentication_course's Introduction

test

This template should help get you started developing with Vue 3 in Vite.

Recommended IDE Setup

VSCode + Volar (and disable Vetur) + TypeScript Vue Plugin (Volar).

Customize configuration

See Vite Configuration Reference.

Project Setup

npm install

Compile and Hot-Reload for Development

npm run dev

Compile and Minify for Production

npm run build

start backend

node server.js

start

npm run start

目标

  1. 注册
  2. 登录
  3. 访问受保护的数据

注册及登录后 vuex 需要做 3 件事

  1. 在 Vuex state 中存储 userData
  2. 在 localstorage 中存储 userData
  3. 在 axios header 中增加 token

登出

  1. 清除 localstorage 中存储的 userData
  2. 清除 Vuex state 中的 userDate 和 axios header 中的 token, 使用 location.reload() 可以实现

navigation guard 仅登录用户可以访问受保护路由

  • /dashboard 路由增加 meta: {requireAthu: true}
  • router.beforeEach 判断当路由有 meta.requireAthu 时是否已登录,
    • 如果未登录, next('/')
    • 如果已登录, next()
  • 参考 Navigation Guards
  • 参考 Route Meta Fields

保持登录

  • 登录后, 刷新页面或者关闭页面标签仍保持登录状态
    • 从 localstorage 取出 userData, 调用 Vuex action

恶意伪造 token 处理

当攻击者试图使用伪造的 token 访问受保护资源, 中断并立即登出

    new Vue({
      router,
      store,
      created () {
    	...
    	axios.interceptors.response.use(
            response => response, // simply return the response 
    		error => {
    		    if (error.response.status === 401) { // if we catch a 401 error
    		     this.$store.dispatch('logout') // force a log out 
    		}
    		return Promise.reject(error) // reject the Promise, with the error as the reason
    	    }
    	)
      },
      render: h => h(App)
    }).$mount('#app')

authentication_course's People

Contributors

kunkgg 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.