Answer the question
In order to leave comments, you need to log in
How to do "Facebook Login"?
I'm using this technology for the first time, so please, without criticism of stupidity - this is still a community of questions after all!
I want to make sure that when the page loads, it checks if the user is logged in or not. Code from the Facebook SDK for JavaScript:
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
The first thing to do when loading a web page is to check if the person is already signed in to your app using Facebook Login. Call FB.getLoginStatus. A call will be sent to Facebook to get the login status and a callback function will be triggered to provide the results.- written on the site at the beginning of work with this. And here is the code of my page, where I want to insert and call this code:
@page "{handler?}"
@model Inst_Post_Searcher.Pages.LoginModel
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
window.fbAsyncInit = function () {
FB.init({
appId: '295566758542379',
autoLogAppEvents: true,
xfbml: true,
version: 'v8.0'
});
FB.AppEvents.logPageView();
};
(function (d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0]; //<--Facebook JS SDK
if (d.getElementById(id)) { return; }
js = d.createElement(s); js.id = id;
js.src = "https://connect.facebook.net/ru_RU/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
<div class="tbody">
<div class="title">
<h1><nobr>Inst-Post Searcher</nobr></h1>
<hr size="2" />
</div>
<div class="condition">
<h2>@Model.Message</h2>
</div>
<form method="post">
<div align="center">
<input class="loginbutton" type="submit" value="Войти" />
</div>
</form>
</div>
</body>
</html>
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
and also I don't know where to call it. using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
namespace Inst_Post_Searcher.Pages
{
public class LoginModel : PageModel
{
public string Message { get; set; }
private readonly ILogger<LoginModel> _logger;
public LoginModel(ILogger<LoginModel> logger)
{
_logger = logger;
}
public void OnGet()
{
Message = "Для использования приложения необходимо иметь уже зарегистрированный аккаунт в Instagram!";
}
public void OnPost()
{
}
}
}
Call FB.getLoginStatus. A call will be sent to Facebook to get the login status and a callback function will be triggered to provide the results.- the moment when we have already sent the request. But here, too, I was not left without questions. How and what variable and where should we create and how to transfer to it
The response object passed to the callback contains several fields:
{
status: 'connected',
authResponse: {
accessToken: '...',
expiresIn:'...',
signedRequest:'...',
userID:'.. .'
}
}
response object? How, after receiving the response object, to access the status field and check it in if and else? Where exactly in the code of my page will I need to write this code? What tag?
status reports the status of a person's login to the application. The state can take one of the following values:
connected - the person is signed in to Facebook and your app.
not_authorized - The person is signed in to Facebook but not signed in to the app.
unknown - The person is not logged in to Facebook, so it is not known if the application is logged in, or if FB.logout() was called earlier and therefore cannot connect to Facebook.
authResponse will be added if the status is connected and consists of the following elements:
accessToken - contains the access token for the application user.
expiresIn - Specifies the UNIX time when the token will expire and need to be renewed.
signedRequest - A signing parameter containing information about the user of the application.
userID - Specifies the application's user ID.
Once the sign-in status is determined, you can choose one of the following options:- how to implement the second "if"?
If the person is signed in to Facebook and the app, allow them to use the app as a registered user.
If the person is not logged into the app or Facebook, offer them a "Login" dialog with the FB.login() element, or show the "Login" button.
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question