SocialAuthentication .NET library verifies the social login access token in the backend & return necessary infomations to identify the user.
The access token has to be fetched in frontend applicaton by signing in thorugh social authentication provider.
Currently this library supports 3 providers- google,apple & facebook
Install Rapplis.SocialAuhentication from NuGet Package Manager.
Install-Package Rapplis.SocialAuhenticationThere is some setup to be done in the frontend part to make this library work smoothly. If you care about only backend, just ignore this section.
-
- Setup OAuth client according to Google Documentation
- Make sure to enable People API in cloud console.
- For android, make sure your app's SHA-1 is setup in cloud console.
- While signin in to google, make sure to use the following scopes:
https://www.googleapis.com/auth/userinfo.profile
https://www.googleapis.com/auth/userinfo.email
https://www.googleapis.com/auth/user.gender.read
https://www.googleapis.com/auth/user.birthday.read
-
- Create an app in Meta for Developers and setup login accroding to the Documentation
- For Android, setup app's hash key in the console.
- While signing in with facebook, add the following permissions:
email,public_profile,user_birthday,user_gender.
-
- Follow official Apple Documentation & get
ClientIdfor apple sign in. - Setup redirect url & deep linking for Android.
- Make sure to add the following scopes:
email,fullName. - You will get user's full name only once when user signs in for the first time. You have to send the name to the backend as Apple is not sending user's full name to backend for safety reasons.
- Follow official Apple Documentation & get
await SocialAuth.GetUser(data);
| Parameter Type | Return Type |
|---|---|
SocialAuthData |
SocialUser |
-
Variable Type Value Google stringgoogleFacebook stringfacebookApple stringapple
-
This model is accepted as peremeter in
GetUsermethod.Field Type Description Provider stringrequired* provider name mentioned in SocialAuthProviderToken stringrequired* access token fetched through signing in social provider AppleClientId stringrequired for apple* client id from apple developer console -
This model is returned by
GetUsermethod.Field Type Description Id stringUnique ID fetched from login provider which has to be used for identifying users Email string?nullableEmail address of the userEmailVerified boolIf email address have verified by social auth provider Name String?nullableUser's full nameFirstName String?nullableFirst Name of the userLastName String?nullableLast Name of the userPicture String?nullableImage url of user's profile pictureLocale String?nullableUser's localeGender String?nullableGender of the userBirthday DateTime?nullableUser's birthdayProvider Stringprovider name mentioned in SocialAuthProvider
First you have to get the access token of a provider from user through api or however you are implementing. Pass the provider name to validate the access token and get a user with unique id provided by the social authentication provider. Here is an example of Google Authentication.
var googleAuthData = new SocialAuthData {
Token = "access_token_got_from_user",
Provider = SocialAuthProvider.Google,
};
SocialUser? socialUser = null;
try
{
socialUser = await SocialAuth.GetUser(googleAuthData);
}
catch (Exception e)
{
//todo handle error
}
The objective of this library is to verify social authentication token and get user data froms social authentication providers in the backend.
Just as beginning, it supports only 3 providers- google, apple & facebook. Hope to implement more providers soon. Even though it is properly tested, it may have some edge case issues. Feel free to submit issue on Github Repository. You're also welcome to contribute to this package.
Follow me for more on github/rafid08