Skip to content

Commit

Permalink
Add Microsoft Identity
Browse files Browse the repository at this point in the history
  • Loading branch information
meghanmae committed May 16, 2024
1 parent 26a6735 commit d1f5b61
Show file tree
Hide file tree
Showing 11 changed files with 1,007 additions and 2 deletions.
47 changes: 47 additions & 0 deletions Wordle.Api/Wordle.Api/Identity/IdentitySeed.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Microsoft.AspNetCore.Identity;
using System.Data;
using System.Security.Claims;
using System;
using Wordle.Api.Models;

namespace Wordle.Api.Identity;
public static class IdentitySeed
{
public static async Task SeedAsync(UserManager<AppUser> userManager, RoleManager<IdentityRole> roleManager, WordleDbContext db)
{
// Seed Roles
await SeedRolesAsync(roleManager);

// Seed Admin User
await SeedAdminUserAsync(userManager);
}

private static async Task SeedRolesAsync(RoleManager<IdentityRole> roleManager)
{
// Seed Roles
if (!await roleManager.RoleExistsAsync(Roles.Admin))
{
await roleManager.CreateAsync(new IdentityRole(Roles.Admin));
}
}

private static async Task SeedAdminUserAsync(UserManager<AppUser> userManager)
{
// Seed Admin User
if (await userManager.FindByEmailAsync("[email protected]") == null)
{
AppUser user = new AppUser
{
UserName = "[email protected]",
Email = "[email protected]"
};

IdentityResult result = userManager.CreateAsync(user, "P@ssw0rd123").Result;

if (result.Succeeded)
{
await userManager.AddToRoleAsync(user, Roles.Admin);
}
}
}
}
8 changes: 8 additions & 0 deletions Wordle.Api/Wordle.Api/Identity/JwtConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Wordle.Api.Identity;
public class JwtConfiguration
{
public required string Secret { get; set; }
public required string Issuer { get; set; }
public required string Audience { get; set; }
public int ExpirationInMinutes { get; set; } = 1440;
}
5 changes: 5 additions & 0 deletions Wordle.Api/Wordle.Api/Identity/Roles.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Wordle.Api.Identity;
public static class Roles
{
public const string Admin = "Admin";
}
Loading

0 comments on commit d1f5b61

Please sign in to comment.