使用Node.js保存到localStorage [英] Saving into localStorage with nodejs

查看:250
本文介绍了使用Node.js保存到localStorage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户登录后(已被授权),我需要通过nodejs将jwt-token保存在本地存储中.

I need to save jwt-token in local-storage through nodejs after the user has logged in (has been authorized ).

检查我的控制器中的用户名/密码是否正确后-将生成的令牌保存在本地存储中.就目前而言,我无法引用窗口,因为它不存在.

After I check if the user/password is correct in my controller - I save generated token in local storage. As it stands I can't reference window as it doesn't exists.

ReferenceError: window is not defined 

这是我目前正在尝试的方式.

This is how I'm currently trying to do it.

...
      payload = {
        sub: user.email,
        role: user.role
      };

      token = jwt.sign(payload, jwtSecretKey, {expiresIn: '60m'});

      window.localStorage.setItem('token', token);

      res.status(200).render('index' , {
        user: user,
        token: token
      });
...

推荐答案

您无法将其保存到Node.js上的 localStorage 中,但可以在浏览器中进行保存,可能是从服务器发送之后,您可以从Node.js上的服务器上获取您的案例.

You cannot save to localStorage on Node.js, but you can do it on a browser, possibly after sending from a server, for your case from a server on Node.js.

您应该将令牌从服务器(在Node.js上运行)发送到客户端(浏览器),该客户端具有一个 window 对象,该对象具有 localStorage 和相关的 getItem setItem 方法,您可以从JavaScript代码中为客户端(浏览器)引用.Node.js没有任何要引用的 window .因此,通过在Node.js代码中引用它,您将遇到一个 undefined 错误.

You should send the token from the server (running on Node.js) to the client (browser) which has a window object, having the localStorage and related getItem and setItem methods, you can reference from your JavaScript code for client (browser). Node.js does not have any window to reference. So, by referencing it in Node.js code you will get an undefined error you have encountered.

只需将其放入Cookie中并发送,或通过json响应发送即可.然后在客户端浏览器上将其保存到 window.localStorage .

Just put it into a cookie and send, or send it via a json response. Then on the client browser save it into window.localStorage.

以下是后一种方法的示例代码;通过响应发送:

following is the examples code for the latter way; sending via a response:

// SERVER-SIDE Code
// say `app` is your app server on node.js
// this is a url handler on your server-side code
// you just have sent your user credentials from a browser
// typically via a form in the body of your http request
app.post('/auth', function (req, res) {
  // you may have here some jwt token creation things
  // ...
  // and send it as your response to the client (probably a web browser) ..
  // with your prefrred name as the key, say 'my_token', ..
  // where you will save it to localStorage (ie. window.localStorage)
  res.json({my_token: 'asdfgh-anything-jw-token-qwerty'})
})


// CLIENT-SIDE Code (may be a web browser)
// You have just sent a request to the server..
// ..with user credentials for authentication in the request body
// in the request body may be a window.FormData object or a json etc.
http.post('auth', userCredentials)
  // probably the request mechanism you make http..
  // ..requests asynchronously, maybe using a library,..
  // ..will return a Promise, and you will have a similar code below.
  .then(response => {
    response.json()
      .then(responseJson => {
        // set localStorage with your preferred name,..
        // ..say 'my_token', and the value sent by server
        window.localStorage.setItem('my_token', responseJson.my_token)
        // you may also want to redirect after you have saved localStorage:
        // window.location.assign("http://www.example.org")
      })
  })

这篇关于使用Node.js保存到localStorage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆