public async Task <IActionResult> Login([FromBody] RegisterCredentialsModel creds) { var user = await _dbContext.AppUsers.FirstOrDefaultAsync(x => x.Email == creds.Email); if (user == null) { return(Json(new BaseResponseModel() { Success = false, Error = "Lietotājs nav atrasts!" })); } if (user.PasswordHash != AuthUtils.GetHashString(creds.Password)) { return(Json(new BaseResponseModel() { Success = false, Error = "Nepareiza parole!" })); } ; return(Json(new LoginResponseModel() { Token = AuthUtils.GenerateJSONWebToken(_configuration["Jwt:Key"], user.Id), Email = user.Email })); }
public async Task <IActionResult> Register([FromBody] RegisterCredentialsModel creds) { var existingUser = await _dbContext.AppUsers.FirstOrDefaultAsync(x => x.Email == creds.Email); if (existingUser != null) { return(Json(new BaseResponseModel() { Success = false, Error = "Epasts aizņemts!" })); } var newUser = new AppUser() { Email = creds.Email, PasswordHash = AuthUtils.GetHashString(creds.Password) }; _dbContext.AppUsers.Add(newUser); await _dbContext.SaveChangesAsync(); return(Json(new BaseResponseModel())); }