Skip to content

Commit

Permalink
Merge pull request #59 from 1-system-group/feature/fix_login_api
Browse files Browse the repository at this point in the history
認証APIの修正
  • Loading branch information
birdsea authored May 19, 2024
2 parents a90ff09 + 8edbb99 commit eba9de8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
16 changes: 11 additions & 5 deletions Diary-Sample/Controllers/ApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public ApiController(ILogger<ApiController> logger, IApiService service,
[HttpPost]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(typeof(BadRequestResult), StatusCodes.Status400BadRequest)]
public async Task<IActionResult> Login(string email, string password, string deviceId)
public ActionResult<string> Login([FromBody] AuthModel model)
{
IdentityUser user = await _userManager.FindByNameAsync(email).ConfigureAwait(false);
IdentityUser user = Task.Run(() => { return _userManager.FindByNameAsync(model.email).ConfigureAwait(false); }).Result.GetAwaiter().GetResult();
if (user == null)
{
return Unauthorized();
Expand All @@ -56,12 +56,18 @@ public async Task<IActionResult> Login(string email, string password, string dev
return Unauthorized();
}

bool isPasswordOk = await _userManager.CheckPasswordAsync(user, password).ConfigureAwait(false);
bool isPasswordOk = Task.Run(() =>
{
return _userManager.CheckPasswordAsync(user, model.password).ConfigureAwait(false);
}).Result.GetAwaiter().GetResult();
if (isPasswordOk)
{
// 認証トークンを発行する
var roles = await _userManager.GetRolesAsync(user).ConfigureAwait(false);
var token = _jwtHandler.GenerateEncodedToken(user.UserName, deviceId, roles);
var roles = Task.Run(() =>
{
return _userManager.GetRolesAsync(user).ConfigureAwait(false);
}).Result.GetAwaiter().GetResult();
var token = _jwtHandler.GenerateEncodedToken(user.UserName, model.deviceId, roles);
return Ok(token);
}

Expand Down
6 changes: 2 additions & 4 deletions Diary-Sample/Controllers/IApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,15 @@ public interface IApiController
/// <remarks>
/// モバイルアプリからログインするためのAPI
/// </remarks>
/// <param name="email"></param>
/// <param name="password"></param>
/// <param name="deviceId"></param>
/// <param name="model"></param>
/// <returns>認証トークン</returns>
/// <response code="200">OK 認証トークン</response>
/// <response code="401">NG 認証失敗</response>
[AllowAnonymous]
[HttpPost]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(typeof(BadRequestResult), StatusCodes.Status400BadRequest)]
public Task<IActionResult> Login(string email, string password, string deviceId);
public ActionResult<string> Login([FromBody] AuthModel model);

/// <summary>
/// 日記の情報一覧を取得する
Expand Down
27 changes: 27 additions & 0 deletions Diary-Sample/Models/AuthModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// -----------------------------------------------------------------------
// <copyright file="AuthModel.cs" company="1-system-group">
// Copyright (c) 1-system-group. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------
using System.ComponentModel.DataAnnotations;
using Diary_Sample.Common;
using static Diary_Sample.Common.ResultType;

namespace Diary_Sample.Models
{
public class AuthModel
{
// Eメールアドレス
[Required(ErrorMessage = "Eメールは必須です。")]
public string email { get; set; } = string.Empty;

// パスワード
[Required(ErrorMessage = "パスワードは必須です")]
[DataType(DataType.Password)]
public string password { get; set; } = string.Empty;

// 端末ID
[Required(ErrorMessage = "端末IDは必須です")]
public string deviceId { get; set; } = string.Empty;
}
}

0 comments on commit eba9de8

Please sign in to comment.