Skip to content

Commit a078ee9

Browse files
Merge pull request #128 from nullinside-development-group/feat/oauth
feat: refresh token
2 parents 42789c7 + a576ba7 commit a078ee9

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/Nullinside.Api/Controllers/UserController.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,29 @@ public UserController(IConfiguration configuration, INullinsideContext dbContext
9393
return Redirect($"{siteUrl}/user/login?error=1");
9494
}
9595
}
96+
97+
/// <summary>
98+
/// Called to generate a new oauth token using the refresh token we previously provided.
99+
/// </summary>
100+
/// <param name="token">The refresh token we provided.</param>
101+
/// <param name="cancellationToken">The cancellation token.</param>
102+
/// <returns>A redirect to the nullinside website.</returns>
103+
[AllowAnonymous]
104+
[HttpPost]
105+
[Route("token/refresh")]
106+
public async Task<ActionResult> Refresh(AuthToken token, CancellationToken cancellationToken = new()) {
107+
var user = await _dbContext.Users.FirstOrDefaultAsync(u => u.RefreshToken == token.Token, cancellationToken).ConfigureAwait(false);
108+
if (null == user?.Email) {
109+
return Unauthorized();
110+
}
111+
112+
var bearerToken = await UserHelpers.GenerateTokenAndSaveToDatabase(_dbContext, user.Email, Constants.OAUTH_TOKEN_TIME_LIMIT, cancellationToken: cancellationToken).ConfigureAwait(false);
113+
if (null == bearerToken) {
114+
return StatusCode(500);
115+
}
116+
117+
return Ok(bearerToken);
118+
}
96119

97120
/// <summary>
98121
/// Converts the credential string we get from google to a representation we read information from.

0 commit comments

Comments
 (0)