Advertisement
Guest User

Untitled

a guest
Sep 16th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.40 KB | None | 0 0
  1. /**
  2. * Handles requests for index page
  3. **/
  4. func firstTimeIndexPage(w http.ResponseWriter, r *http.Request) {
  5.  
  6.     if controller.IsSessionValid(r) {
  7.         t := template.Must(template.ParseFiles("static/templates/basicLayout.html"))
  8.         t.ExecuteTemplate(w, "indexpage", PageInfo)
  9.     } else {
  10.  
  11.         http.Redirect(w, r, "/login", 302)
  12.     }
  13. }
  14.  
  15. func loginPage(w http.ResponseWriter, r *http.Request) {
  16.     t := template.Must(template.ParseFiles("static/templates/startPage.html"))
  17.  
  18.     t.ExecuteTemplate(w, "loginpage", PageInfo)
  19. }
  20.  
  21. func indexPage(w http.ResponseWriter) {
  22.     t := template.Must(template.ParseFiles("static/templates/basicLayout.html"))
  23.     t.ExecuteTemplate(w, "indexpage", PageInfo)
  24. }
  25.  
  26. func Authenticate(w *http.ResponseWriter, houseName string, userName string, password string) bool {
  27.  
  28.     isLegit := false
  29.     if controller.IsEntryInDb("users", "WHERE user_name='"+userName+"' AND user_password='"+password+"';") && controller.IsEntryInDb("users", "WHERE user_name='"+userName+"' AND house_name='"+houseName+"';") {
  30.         userId := controller.GetUserId(userName)
  31.         createSession(w, houseName, userId)
  32.         isLegit = true
  33.     } else {
  34.         isLegit = false
  35.     }
  36.  
  37.     return isLegit
  38. }
  39.  
  40. func createSession(w *http.ResponseWriter, houseName string, userId string) {
  41.  
  42.     expire := time.Now().AddDate(0, 0, 1)
  43.     SessionId, err := uuid.NewUUID()
  44.     sessionIdString := SessionId.String()
  45.  
  46.     if err != nil {
  47.         fmt.Println(err)
  48.     }
  49.  
  50.     cookieUserId := http.Cookie{
  51.         Name:     "smarthomedUserId",
  52.         Value:    userId,
  53.         Path:     "/",
  54.         MaxAge:   10000,
  55.         Expires:  expire,
  56.         HttpOnly: false,
  57.     }
  58.     cookieSessionId := http.Cookie{
  59.         Name:     "smarthomedSessionId",
  60.         Value:    sessionIdString,
  61.         Path:     "/",
  62.         MaxAge:   10000,
  63.         Expires:  expire,
  64.         HttpOnly: false,
  65.     }
  66.     cookieHouseName := http.Cookie{
  67.         Name:     "smarthomedHouseName",
  68.         Value:    houseName,
  69.         Path:"/",
  70.         MaxAge:   10000,
  71.         Expires:  expire,
  72.         HttpOnly: false,
  73.     }
  74.     http.SetCookie(*w, &cookieUserId)
  75.     http.SetCookie(*w, &cookieSessionId)
  76.     http.SetCookie(*w, &cookieHouseName)
  77.     valueString := "'" + userId + "', '" + SessionId.String() + "'"
  78.     if controller.IsEntryInDb("sessions", "WHERE user_id='"+userId+"';") {
  79.         controller.UpdateDbEntry("sessions", "session_id", "'"+SessionId.String()+"'", "WHERE user_id='"+userId+"';")
  80.  
  81.     } else {
  82.         if controller.InsertNewDbEntry("sessions", "user_id, session_id", valueString) {
  83.             fmt.Println("session created.")
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement