/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package auth;

import static model.Hash.hashPassword;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Chiib_000
 */
@WebServlet(name = "Sign_inServlet", urlPatterns = {"/Sign_inServlet"})
public class Sign_inServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     * @throws java.sql.SQLException
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
            String sql_pstm = "SELECT user_id FROM user WHERE user_id = ? AND password = ?";
            
            ServletContext ctx = getServletContext();
            Connection conn = (Connection) ctx.getAttribute("connection");
            PreparedStatement pstm = conn.prepareStatement(sql_pstm);
            
            pstm.setString(1, request.getParameter("username"));
            pstm.setString(2, hashPassword(request.getParameter("password")));

            ResultSet rs = pstm.executeQuery();
            
            if(!rs.next()){
                //add warning text when incorrect ************************
                /*
                
                */
                response.sendRedirect("non-auth/auth.jsp");
            }else{

                //give a token*******************
                /*
                
                */
                response.sendRedirect("/Usami");
            }
        
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(Sign_inServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(Sign_inServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

@@ -814,16 +814,40 @@
 ession;%0D
+%0Aimport model.Profiles;%0D
 %0A%0D%0A/**%0D%0A
@@ -2049,32 +2049,103 @@
 %0D%0A            %0D%0A
+            HttpSession session = request.getSession();%0D%0A            %0D%0A
             if(!
@@ -2157,16 +2157,16 @@
 xt())%7B%0D%0A
-
         
@@ -2334,25 +2334,14 @@
 ct(%22
-non-auth/auth.jsp
+/Usami
 %22);%0D
@@ -2464,32 +2464,196 @@
             */%0D%0A
+                %0D%0A                Profiles profile = new Profiles(conn, request.getParameter(%22username%22));%0D%0A                session.setAttribute(%22user%22, profile);%0D%0A
                 
@@ -2667,38 +2667,41 @@
 e.sendRedirect(%22
-/Usami
+index.jsp
 %22);%0D%0A           

add session & create profiles