public async Task <ActionResult> Login(User paramUser) { // Check to make sure that email exists UserService us = new UserService(_context); var tokenStr = us.Authenticate(paramUser, _config); // Can't process if username is not found if (string.IsNullOrEmpty(tokenStr)) { return(JsonRes.Create(422, new { message = "username not found." })); } // Return token return(JsonRes.Create(200, new { token = tokenStr })); }
public async Task <ActionResult> PostUser([FromBody] User user) { // TODO: Check Username and Password for patterns if (string.IsNullOrEmpty(user.Username) || string.IsNullOrEmpty(user.Password)) { return(JsonRes.Create(422, new { message = "username and password required." })); } UserService us = new UserService(_context); var newUser = us.CreateUser(user.Username, user.Password).Result; if (newUser == null) { return(JsonRes.Create(422, new { message = "username already exists." })); } var tokenStr = Auth.GenerateJSONWebToken(newUser, _config); // Return Success return(JsonRes.Create(201, new { username = newUser.Username, token = tokenStr })); }