package com.tymiller.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.tymiller.model.User;
import com.tymiller.service.UserService;

@RestController
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private UserService userService;
	
	@RequestMapping(value="/login", method = RequestMethod.POST)
	public String login() { return "test";}
	
	@RequestMapping(value="/register", method = RequestMethod.POST)	
	public User registerUser(@RequestBody User user) {
		return userService.save(user);
	}
}

@@ -23,24 +23,110 @@
 ontroller;%0A%0A
+import java.util.Date;%0Aimport java.util.Map;%0A%0Aimport javax.servlet.ServletException;%0A%0A
 import org.s
@@ -176,16 +176,16 @@
 owired;%0A
-
 import o
@@ -499,16 +499,89 @@
 rvice;%0A%0A
+import io.jsonwebtoken.Jwts;%0Aimport io.jsonwebtoken.SignatureAlgorithm;%0A%0A
 @RestCon
@@ -780,26 +780,771 @@
 gin(
-) %7B return %22test%22;
+@RequestBody Map%3CString, String%3E json) throws ServletException %7B%0A%09%09if (json.get(%22username%22) == null %7C%7C json.get(%22password%22) == null) %7B%0A%09%09%09throw new ServletException(%22Please fill in username and password%22);%0A%09%09%7D%0A%09%09%0A%09%09String userName = json.get(%22username%22);%0A%09%09String password = json.get(%22password%22);%0A%09%09%0A%09%09User user = userService.findByUserName(userName);%0A%09%09%0A%09%09if (user == null) %7B%0A%09%09%09throw new ServletException(%22User name not found%22);%0A%09%09%7D%0A%09%09%0A%09%09String pwd = user.getPassword();%0A%09%09%0A%09%09if (!password.equals(pwd)) %7B%0A%09%09%09throw new ServletException(%22Invalid login. Please check your username and password%22);%0A%09%09%7D%0A%09%09%0A%09%09return Jwts.builder().setSubject(userName).claim(%22roles%22, %22user%22).setIssuedAt(new Date())%0A%09%09%09%09.signWith(SignatureAlgorithm.HS256, %22secretkey%22).compact();%0A%09%09%0A%09
 %7D%0A%09%0A

add login function to user controller