Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
H
HIAST-Clinics
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
almohanad.hafez
HIAST-Clinics
Commits
13b39d39
Commit
13b39d39
authored
Aug 21, 2024
by
Almouhannad
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(B) Modify JWT provider and login response
parent
9bec7b49
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
26 additions
and
29 deletions
+26
-29
IJWTProvider.cs
Clinics.Backend/Application/Abstractions/JWT/IJWTProvider.cs
+2
-1
LoginCommandHandler.cs
...d/Application/Users/Commands/Login/LoginCommandHandler.cs
+11
-10
LoginResponse.cs
...Backend/Application/Users/Commands/Login/LoginResponse.cs
+3
-14
JWTProvider.cs
...nd/Persistence/Identity/Authentication/JWT/JWTProvider.cs
+10
-4
No files found.
Clinics.Backend/Application/Abstractions/JWT/IJWTProvider.cs
View file @
13b39d39
using
Domain.Entities.Identity.Users
;
using
Domain.Entities.Identity.Users
;
using
Domain.Entities.People.Shared
;
namespace
Application.Abstractions.JWT
;
namespace
Application.Abstractions.JWT
;
public
interface
IJWTProvider
public
interface
IJWTProvider
{
{
string
Generate
(
User
user
);
string
Generate
(
User
user
,
PersonalInfo
?
personalInfo
=
null
);
}
}
Clinics.Backend/Application/Users/Commands/Login/LoginCommandHandler.cs
View file @
13b39d39
...
@@ -33,37 +33,38 @@ public class LoginCommandHandler : CommandHandlerBase<LoginCommand, LoginRespons
...
@@ -33,37 +33,38 @@ public class LoginCommandHandler : CommandHandlerBase<LoginCommand, LoginRespons
return
Result
.
Failure
<
LoginResponse
>(
IdentityErrors
.
PasswordMismatch
);
return
Result
.
Failure
<
LoginResponse
>(
IdentityErrors
.
PasswordMismatch
);
#
endregion
#
endregion
#
region
2.
Generate
JWT
User
user
=
loginResult
.
Value
!;
User
user
=
loginResult
.
Value
!;
string
token
=
_jwtProvider
.
Generate
(
user
);
#
endregion
#
region
3
.
Generate
Response
#
region
2
.
Generate
Response
#
region
3
.1
.
Admin
#
region
2
.1
.
Admin
if
(
user
.
Role
==
Roles
.
Admin
)
if
(
user
.
Role
==
Roles
.
Admin
)
{
{
return
LoginResponse
.
GetResponse
(
user
,
token
);
var
token
=
_jwtProvider
.
Generate
(
user
);
return
LoginResponse
.
GetResponse
(
token
);
}
}
#
endregion
#
endregion
#
region
3
.2
.
Doctor
#
region
2
.2
.
Doctor
if
(
user
.
Role
==
Roles
.
Doctor
)
if
(
user
.
Role
==
Roles
.
Doctor
)
{
{
var
doctorUserResult
=
await
_userRepository
.
GetDoctorUserByUserNameFullAsync
(
user
.
UserName
);
var
doctorUserResult
=
await
_userRepository
.
GetDoctorUserByUserNameFullAsync
(
user
.
UserName
);
if
(
doctorUserResult
.
IsFailure
)
if
(
doctorUserResult
.
IsFailure
)
return
Result
.
Failure
<
LoginResponse
>(
doctorUserResult
.
Error
);
return
Result
.
Failure
<
LoginResponse
>(
doctorUserResult
.
Error
);
return
LoginResponse
.
GetResponse
(
doctorUserResult
.
Value
.
User
,
token
,
doctorUserResult
.
Value
.
Doctor
.
PersonalInfo
);
var
token
=
_jwtProvider
.
Generate
(
user
,
doctorUserResult
.
Value
.
Doctor
.
PersonalInfo
);
return
LoginResponse
.
GetResponse
(
token
);
}
}
#
endregion
#
endregion
#
region
3
.3
.
Receptionist
user
#
region
2
.3
.
Receptionist
user
if
(
user
.
Role
==
Roles
.
Receptionist
)
if
(
user
.
Role
==
Roles
.
Receptionist
)
{
{
var
receptionistUser
=
await
_userRepository
.
GetReceptionistUserByUserNameFullAsync
(
user
.
UserName
);
var
receptionistUser
=
await
_userRepository
.
GetReceptionistUserByUserNameFullAsync
(
user
.
UserName
);
if
(
receptionistUser
.
IsFailure
)
if
(
receptionistUser
.
IsFailure
)
return
Result
.
Failure
<
LoginResponse
>(
receptionistUser
.
Error
);
return
Result
.
Failure
<
LoginResponse
>(
receptionistUser
.
Error
);
return
LoginResponse
.
GetResponse
(
receptionistUser
.
Value
.
User
,
token
,
receptionistUser
.
Value
.
PersonalInfo
);
var
token
=
_jwtProvider
.
Generate
(
user
,
receptionistUser
.
Value
.
PersonalInfo
);
return
LoginResponse
.
GetResponse
(
token
);
}
}
#
endregion
#
endregion
...
...
Clinics.Backend/Application/Users/Commands/Login/LoginResponse.cs
View file @
13b39d39
...
@@ -8,27 +8,16 @@ namespace Application.Users.Commands.Login;
...
@@ -8,27 +8,16 @@ namespace Application.Users.Commands.Login;
public
class
LoginResponse
public
class
LoginResponse
{
{
public
int
Id
{
get
;
set
;
}
public
string
UserName
{
get
;
set
;
}
=
null
!;
public
string
JWT
{
get
;
set
;
}
=
null
!;
public
string
JWT
{
get
;
set
;
}
=
null
!;
public
string
FullName
{
get
;
set
;
}
=
null
!;
public
static
Result
<
LoginResponse
>
GetResponse
(
User
user
,
string
jwt
,
PersonalInfo
?
personalInfo
=
null
)
public
static
Result
<
LoginResponse
>
GetResponse
(
string
jwt
)
{
{
if
(
jwt
is
null
)
return
Result
.
Failure
<
LoginResponse
>(
IdentityErrors
.
NotFound
);
var
response
=
new
LoginResponse
var
response
=
new
LoginResponse
{
{
Id
=
user
.
Id
,
UserName
=
user
.
UserName
,
JWT
=
jwt
JWT
=
jwt
};
};
if
(
personalInfo
is
null
)
{
if
(
user
.
Role
!=
Roles
.
Admin
)
return
Result
.
Failure
<
LoginResponse
>(
IdentityErrors
.
NotFound
);
response
.
FullName
=
Roles
.
AdminName
;
return
response
;
}
response
.
FullName
=
personalInfo
.
FullName
;
return
response
;
return
response
;
}
}
}
}
Clinics.Backend/Persistence/Identity/Authentication/JWT/JWTProvider.cs
View file @
13b39d39
using
Application.Abstractions.JWT
;
using
Application.Abstractions.JWT
;
using
Domain.Entities.Identity.Users
;
using
Domain.Entities.Identity.Users
;
using
Domain.Entities.People.Shared
;
using
Microsoft.Extensions.Options
;
using
Microsoft.Extensions.Options
;
using
Microsoft.IdentityModel.Tokens
;
using
Microsoft.IdentityModel.Tokens
;
using
System.IdentityModel.Tokens.Jwt
;
using
System.IdentityModel.Tokens.Jwt
;
...
@@ -17,15 +18,20 @@ public sealed class JWTProvider : IJWTProvider
...
@@ -17,15 +18,20 @@ public sealed class JWTProvider : IJWTProvider
_options
=
options
.
Value
;
_options
=
options
.
Value
;
}
}
public
string
Generate
(
User
user
)
public
string
Generate
(
User
user
,
PersonalInfo
?
personalInfo
=
null
)
{
{
var
claims
=
new
Claim
[]
var
claims
=
new
List
<
Claim
>
{
{
new
(
"Id"
,
user
.
Id
.
ToString
()),
new
(
ClaimTypes
.
Name
,
user
.
UserName
),
new
(
ClaimTypes
.
Name
,
user
.
UserName
),
new
(
ClaimTypes
.
Role
,
user
.
Role
.
Name
)
new
(
ClaimTypes
.
Role
,
user
.
Role
.
Name
)
};
};
if
(
personalInfo
is
not
null
)
{
claims
.
Add
(
new
Claim
(
"FullName"
,
personalInfo
.
FullName
));
}
var
signingCredentials
=
new
SigningCredentials
(
var
signingCredentials
=
new
SigningCredentials
(
new
SymmetricSecurityKey
(
new
SymmetricSecurityKey
(
Encoding
.
UTF8
.
GetBytes
(
_options
.
SecretKey
)),
Encoding
.
UTF8
.
GetBytes
(
_options
.
SecretKey
)),
...
@@ -34,7 +40,7 @@ public sealed class JWTProvider : IJWTProvider
...
@@ -34,7 +40,7 @@ public sealed class JWTProvider : IJWTProvider
var
token
=
new
JwtSecurityToken
(
var
token
=
new
JwtSecurityToken
(
_options
.
Issuer
,
_options
.
Issuer
,
_options
.
Audience
,
_options
.
Audience
,
claims
,
claims
.
ToArray
()
,
null
,
null
,
DateTime
.
UtcNow
.
AddDays
(
30
),
DateTime
.
UtcNow
.
AddDays
(
30
),
signingCredentials
);
signingCredentials
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment