public async Task <bool> CheckUserAuthentication(HttpRequest request) { if (request.Headers.ContainsKey("access-token")) { string token = request.Headers["access-token"]; using (IDbConnection conn = Connection) { conn.Open(); LoginResponseInfo info = await GetAuthenticationByToken(token); if (info != null) { if (info.Expires > DateTime.Now) { return(true); } else { return(false); } } else { return(false); } } } else { return(false); } }
public async Task <LoginResponseInfo> AuthenticateUser(LoginRequestInfo info) { User user = await GetUserByEmail(info.Email); if (user != null) { if (user.PasswordHash == info.PasswordHash) { LoginResponseInfo loginInfo = await GetAuthenticationByEmail(user.Email); if (loginInfo != null) { if (loginInfo.Expires > DateTime.Now) { return(loginInfo); } else { await RemoveAuthenticationByToken(loginInfo.Token); LoginResponseInfo responseInfo = await CreateAuthentication(user.Id); return(responseInfo); } } else { LoginResponseInfo responseInfo = await CreateAuthentication(user.Id); return(responseInfo); } } else { throw new BadLoginDataException(); } } else { throw new NotFoundException(); } }
private async Task <LoginResponseInfo> CreateAuthentication(int userId) { string token = Guid.NewGuid().ToString(); DateTime created = DateTime.Now; DateTime expires = DateTime.Now.AddDays(1); using (IDbConnection conn = Connection) { conn.Open(); await conn.ExecuteAsync(addUserAuthenticationInfo, new { UserId = userId, Token = token, Created = created, Expires = expires }); LoginResponseInfo responseInfo = new LoginResponseInfo() { UserId = userId, Token = token, Created = created, Expires = expires }; return(responseInfo); } }