Detailed NUXT Routing Authentication (Express Template)

Here we use user login authentication as an example

Express relies on Express-session middleware to implement session function

If we do not load Express-session components

 Router.get ('/ user / login ", function (req, res) {console.log (req, req.session)}   
will find that there is no session object

introduced into the Express-session component

 // server / index.js file .. .import session from 'express-session' ... app.use (session) ...   
and then try

Router.get (‘/ user / login “, function (req, res) {console.log (req.session)})
You can see that the empty array object is printed. . . Ok, let’s use

Import {router} from ‘Express’Const Router = router () Import Request from’ SuperAgent’Router.get (‘/ user / login ‘, function (req, res) {request .get (path).query (REQ.QUERY) .SET (‘Content-Type’, ‘Application / Json; Charset = UTF-8’) .end ((Err, _res) => {if (_res.body.code === 200 ) {req.session.authuser = // save the user data returned to session} res.}}
   Seeesion, how to conduct authentication? 
First, save the user session to the Store for global use

nuxTserverinit method: If the nuXTserverinit method is specified in the status tree, nuXT. JS When calling it, the context object will be passed to it as the second parameter (sauce sauce when the server is called). This method is easy to use when we want to pass some of the data from some data to the client.

For example, assuming that our server’s session state tree can access the current logged in by Req.Session.user. Put the login user information to the client’s status tree, we only need to update Store / Index.js as follows:

Actions: {nuXTserverinit ({commit}, {req} ) {if (req.session.user) {commit (‘user’, req.session.user)}}}
  So we can use the USER in the STORE Status to know if the user is logged in.  
Page adding middleware when jumps:

Export Default {middleware: author}

Export Default Function ({store, redirect}) {if (! store.state.authuser) {return redirect (‘/ user / login “}}}}

  A-> b MiddleWare authentication is not logged in, jump login page login, call the login interface to save the user session, return A, jump again B, NUXTSERVERINIT Gets the session from the context object in Store, the authentication is successful, and the jump is successful.  
Expand: Connect-Redis

Sever / INDEX configuration

Import Express from ‘express’import {Nuxt, Builder} from’ nuxt’import session from ‘express-session’import redis from’ connect-redis’import bodyParser from ‘body-parser’import api from’ ./api’import serverConfig from ‘ ./config ‘// redis configuration file const ax = expression () const host = || ‘’const port = process.env.port || 3000const redisstore = redis (session) app.use (bodyparser.json ()) app.use (‘ / static ‘, express.static) ‘static’)) app.use ({Name: ‘SID’, Secret: ‘Asecret123-‘, ResAVe: True, Rolling: true, SaveunInitialized: False, Store: New Redisstore (ServerConfig)}) App.set (‘Port’, Port) // Import API Routesapp.use (‘/ API’, API) App.Use (‘/’, API) // Import and Set nuXt.js Optionslet config = required (‘../ nuXT .config.js’) =! (Process.env.node_env === ‘product’) // init nuxt.jsconst nuXT = new nuXT (config) // build only in dev modeif ( { Const builder = new builder (nuxt) ()} // give nuXT middleware to expressapp.use (nuXt.render) // listen the Serverapp.listen (port, host) Console.loG (‘server listening on’ + host + ‘:’ + port) // eslint-disable-line no-console

The above is all the contents of this article, it is desirable Everyone’s learning is helpful, I hope everyone will support Tumi Clouds.
© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment