/auth-auth-easy

.NET Standart library that handles Authorization/Authentication methods. More info at: https://www.youtube.com/watch?v=dQw4w9WgXcQ

Primary LanguageC#

Auth Auth Easy

.NET Standart library that handles Authorization/Authentication methods by using NoSQL (MongoDB), SQL (MSSQL) database. It also can use Redis as cache.

Usage

[OUTDATED] -New documentation coming soon.

Creating User Model

using AuthAuthEasyLib.Bases;

public class UserModel : AuthUser // Must use AuthUser class as base class
{
 // Add your additional properties that does not included in AuthUser class.
}

Instantiating AuthService

using AuthAuthEasyLib.Services; // Add using statement to access services namespace

 var mongoConfig = new MongoCrudServiceConfig(
                "CONNECTION STRING",
                "DATABASE NAME",
                "COLECTION NAME");
// or

MongoClientSettings settings = new MongoClientSettings(); // Then configure settings
var mongoConfig = new MongoCrudServiceConfig(
                settings,
                "DATABASE NAME",
                "COLECTION NAME");

var authService = new AuthService<UserModel>(mongoConfig); // Instantiate new Auth Service with the generic type of your UserModel.
//Note: UserModel class must use AuthUser class as base.

Methods ( Async methods are also available. )

Register

var newUser = new UserModel() {Username = "USERNAME", Password = "PASSWORD"};
authService.Register(newUser);
Login
var (loggedInUser, authToken) = authService.LoginWithUsername("USERNAME", "PASSWORD"); // Returns a tuple of UserModel and Token
// or
var (loggedInUser, authToken) = authService.Login(user => 
                                      user.PhoneNumber == "PHONE NUMBER", 
                                      "PASSWORD"); // You can log in with any identifier. // Returns a tuple of UserModel and Token

Verification

                                      
// Creates or gets  verification token for user with the expiration time provided.
var verificationToken = authService.GetOrCreateVerificationToken("USER ID", new Timespan(7,0,0,0)) 
// Verifies User
authService.VerifyUser(verificationToken.Key);

Logout

authService.Logout(authToken.Key);

// Logout all sessions:
var user = authService.GetUserById("USER ID");
foreach (var token in user.Tokens.Where(t => t.TokenCode == 1)) // TokenCode 1 for auth Tokens
{
    authService.LogOut(token.Key);
}

IsAuthorized

var (isAuthorized, Except, User) = authService.IsAuthorized("AUTH TOKEN KEY"); 
// or 
var (isAuthorized, Except, User) = authService.IsAuthorized("AUTH TOKEN KEY","admin",true); // Requires admin role case insensitive
if(isAuthorized)
{
  // User Is Authorized
}else
{
  throw Except;
}

Change Password

authService.ChangePasswordAsync("USER ID", "OLD PASSWORD", "NEW PASSWORD");
//or
authService.ChangePasswordAsync(user =>
                                user.Username == "USERNAME",
                                "OLD PASSWORD", 
                                "NEW PASSWORD");

Reset Password

var passwordResetToken = authService.AddPaswordResetRequest(user => 
                                                            user._Id == "USER ID",
                                                            new TimeSpan(7,0,0,0)); // Adds Password Reset Token to found user
authService.ResetPassword(passwordResetToken.Key, "NEW PASSWORD");              

Update User Info

authService.UpdateUser("USER ID", user => user.Username = "NEW USERNAME");