简体   繁体   English

是否可以检查 Parse 用户是否从服务器端登录?

[英]Is it possible to check is Parse user is logged in from the server side?

I am logging in/signing up users client side but I would like to check if their logged in from the server upon a page landing get request.我正在登录/注册用户客户端,但我想检查他们是否在页面登陆获取请求时从服务器登录。

Is this possible?这可能吗?

您应该有一些服务器端来检查用户是否经过身份验证,这是一种安全的方式,因为您不能信任验证客户端,因为用户可以更改客户端代码。

You can create a Cloud Code function to check if the user is logged In.您可以创建一个 Cloud Code 函数来检查用户是否已登录。 Some simple example:一些简单的例子:

Parse.Cloud.define('isLoggedIn', async (req) => {
  const { user } = req;

  if (!user) {
    return { isLogged: false };
  }

  const sessionToken = user.getSessionToken();

  try {
    const result = await Parse.Cloud.httpRequest({
      url: `${Parse.serverURL}/sessions/me`,
      headers: {
        "X-Parse-Application-Id": Parse.applicationId,
        "X-Parse-JavaScript-API-Key": Parse.javaScriptKey,
        "X-Parse-Session-Token": sessionToken,
      }
    });

    return { isLogged: true }
  } catch (e) {
    return { isLogged: false }
  }
});

Then you can use this cloud code function like this:然后你可以像这样使用这个云代码功能:

const isLoggedInFunc = async () => {
  const { isLoggedIn } = await Parse.Cloud.run("isLoggedIn");

  if (isLoggedIn) {
    // if logged in do what you want
  } else {
    // if not logged in do what you want
    // for example redirect user to login page or something ...
  }
}

Hope this will help :)希望这会有所帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM