Skip to content

Commit

Permalink
Merge pull request #37 from AmielCyber/update
Browse files Browse the repository at this point in the history
Updated problem details library
  • Loading branch information
AmielCyber authored Aug 16, 2023
2 parents 053d71d + 7870837 commit 72c5857
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 44 deletions.
62 changes: 31 additions & 31 deletions ReactRoastDotnet.API/Services/TokenService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,45 @@ namespace ReactRoastDotnet.API.Services;

public class TokenService
{
private readonly UserManager<User> _userManager;
private readonly IConfiguration _configuration;

public TokenService(UserManager<User> userManager, IConfiguration configuration)
private readonly UserManager<User> _userManager;
private readonly IConfiguration _configuration;

public TokenService(UserManager<User> userManager, IConfiguration configuration)
{
_userManager = userManager;
_configuration = configuration;
}

public async Task<string> GenerateToken(User user)
{
if (string.IsNullOrEmpty(user.UserName) || string.IsNullOrEmpty(user.Email))
{
_userManager = userManager;
_configuration = configuration;
throw new Exception("Invalid user email or username passed for token creation.");
}

public async Task<string> GenerateToken(User user)
{
if (string.IsNullOrEmpty(user.UserName) || string.IsNullOrEmpty(user.Email))
{
throw new Exception("Invalid user email or username passed for token creation.");
}

var claims = new List<Claim>
var claims = new List<Claim>
{
new Claim(ClaimTypes.Email, user.Email),
new Claim(ClaimTypes.Name, user.Email),
new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
};
var roles = await _userManager.GetRolesAsync(user);
foreach (var role in roles)
{
claims.Add(new Claim(ClaimTypes.Role, role));
}
var roles = await _userManager.GetRolesAsync(user);
foreach (var role in roles)
{
claims.Add(new Claim(ClaimTypes.Role, role));
}

var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["JWTSettings:TokenKey"]));
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha512);
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["JWTSettings:TokenKey"]));

Check warning on line 40 in ReactRoastDotnet.API/Services/TokenService.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 's' in 'byte[] Encoding.GetBytes(string s)'.

Check warning on line 40 in ReactRoastDotnet.API/Services/TokenService.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 's' in 'byte[] Encoding.GetBytes(string s)'.
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha512);

var tokenOptions = new JwtSecurityToken(
issuer: null,
audience: null,
claims: claims,
expires: DateTime.Now.AddDays(7),
signingCredentials: creds
);
var tokenOptions = new JwtSecurityToken(
issuer: null,
audience: null,
claims: claims,
expires: DateTime.Now.AddDays(7),
signingCredentials: creds
);

return new JwtSecurityTokenHandler().WriteToken(tokenOptions);
}
}
return new JwtSecurityTokenHandler().WriteToken(tokenOptions);
}
}
12 changes: 6 additions & 6 deletions ReactRoastDotnet.Client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ReactRoastDotnet.Client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"dependencies": {
"@headlessui/react": "^1.7.15",
"problem-details-mapper": "^1.0.0",
"extract-problem-details": "^1.0.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-hook-form": "^7.45.2",
Expand Down
2 changes: 1 addition & 1 deletion ReactRoastDotnet.Client/src/api/getRequest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import toast from "react-hot-toast";
// My imports.
import getProblemDetails from "problem-details-mapper";
import getProblemDetails from "extract-problem-details";
import problemToast from "../toast/problemToast.tsx";

const baseURL = import.meta.env.VITE_API_URL as string;
Expand Down
2 changes: 1 addition & 1 deletion ReactRoastDotnet.Client/src/models/Result.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// My import.
import type {ProblemDetails} from "problem-details-mapper";
import type {ProblemDetails} from "extract-problem-details";

type Result<T, P = ProblemDetails> =
| { ok: true; value: T }
Expand Down
2 changes: 1 addition & 1 deletion ReactRoastDotnet.Client/src/store/userActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type AuthUser from "../models/AuthUser";
import type LoginRequest from "../models/LoginRequest.ts";
import type Result from "../models/Result.ts";
import type UserSignUpRequest from "../models/UserSignUpRequest.ts";
import getProblemDetails from "problem-details-mapper";
import getProblemDetails from "extract-problem-details";

const BASE_URL = import.meta.env.VITE_API_URL as string;
const ACCOUNT_LOGIN_URL = BASE_URL + "account/login";
Expand Down
2 changes: 1 addition & 1 deletion ReactRoastDotnet.Client/src/store/userStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {create} from "zustand";
// My imports.
import type AuthUser from "../models/AuthUser";
import type LoginRequest from "../models/LoginRequest.ts";
import type {ProblemDetails} from "problem-details-mapper";
import type {ProblemDetails} from "extract-problem-details";
import {signIn} from "./userActions.ts";
import problemToast from "../toast/problemToast.tsx";

Expand Down
2 changes: 1 addition & 1 deletion ReactRoastDotnet.Client/src/toast/ProblemDetailsToast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {ReactNode} from "react";
import type {Toast} from "react-hot-toast";
import toast from "react-hot-toast";
// My imports.
import type {ProblemDetails} from "problem-details-mapper";
import type {ProblemDetails} from "extract-problem-details";
import XMarkIcon from "../icons/XMarkIcon.tsx";

type Props = {
Expand Down
2 changes: 1 addition & 1 deletion ReactRoastDotnet.Client/src/toast/problemToast.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import toast, {ErrorIcon} from "react-hot-toast";
// My Imports.
import type {ProblemDetails} from "problem-details-mapper";
import type {ProblemDetails} from "extract-problem-details";
import ProblemDetailsToast from "./ProblemDetailsToast.tsx";

function problemToast(problemDetails: ProblemDetails) {
Expand Down

0 comments on commit 72c5857

Please sign in to comment.