Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

FacebookAuthenticationRegistrar.cs

using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Facebook;
using Microsoft.AspNetCore.Authentication.OAuth;
using Microsoft.Extensions.DependencyInjection;
using Nop.Core.Infrastructure;
using Nop.Services.Authentication.External;

namespace Nop.Plugin.ExternalAuth.Facebook.Infrastructure;

/// 
/// Represents registrar of Facebook authentication service
/// 
public class FacebookAuthenticationRegistrar : IExternalAuthenticationRegistrar
{
    /// 
    /// Configure
    /// 
    /// Authentication builder
    public void Configure(AuthenticationBuilder builder)
    {
        builder.AddFacebook(FacebookDefaults.AuthenticationScheme, options =>
        {
            //set credentials
            var settings = EngineContext.Current.Resolve();

            options.AppId = string.IsNullOrEmpty(settings?.ClientKeyIdentifier) ? nameof(options.AppId) : settings.ClientKeyIdentifier;
            options.AppSecret = string.IsNullOrEmpty(settings?.ClientSecret) ? nameof(options.AppSecret) : settings.ClientSecret;

            //store access and refresh tokens for the further usage
            options.SaveTokens = true;

            //set custom events handlers
            options.Events = new OAuthEvents
            {
                //in case of error, redirect the user to the specified URL
                OnRemoteFailure = context =>
                {
                    context.HandleResponse();

                    var errorUrl = context.Properties.GetString(FacebookAuthenticationDefaults.ErrorCallback);
                    context.Response.Redirect(errorUrl);

                    return Task.FromResult(0);
                }
            };
        });
    }
}