Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Handles requests for index page
- **/
- func firstTimeIndexPage(w http.ResponseWriter, r *http.Request) {
- if controller.IsSessionValid(r) {
- t := template.Must(template.ParseFiles("static/templates/basicLayout.html"))
- t.ExecuteTemplate(w, "indexpage", PageInfo)
- } else {
- http.Redirect(w, r, "/login", 302)
- }
- }
- func loginPage(w http.ResponseWriter, r *http.Request) {
- t := template.Must(template.ParseFiles("static/templates/startPage.html"))
- t.ExecuteTemplate(w, "loginpage", PageInfo)
- }
- func indexPage(w http.ResponseWriter) {
- t := template.Must(template.ParseFiles("static/templates/basicLayout.html"))
- t.ExecuteTemplate(w, "indexpage", PageInfo)
- }
- func Authenticate(w *http.ResponseWriter, houseName string, userName string, password string) bool {
- isLegit := false
- if controller.IsEntryInDb("users", "WHERE user_name='"+userName+"' AND user_password='"+password+"';") && controller.IsEntryInDb("users", "WHERE user_name='"+userName+"' AND house_name='"+houseName+"';") {
- userId := controller.GetUserId(userName)
- createSession(w, houseName, userId)
- isLegit = true
- } else {
- isLegit = false
- }
- return isLegit
- }
- func createSession(w *http.ResponseWriter, houseName string, userId string) {
- expire := time.Now().AddDate(0, 0, 1)
- SessionId, err := uuid.NewUUID()
- sessionIdString := SessionId.String()
- if err != nil {
- fmt.Println(err)
- }
- cookieUserId := http.Cookie{
- Name: "smarthomedUserId",
- Value: userId,
- Path: "/",
- MaxAge: 10000,
- Expires: expire,
- HttpOnly: false,
- }
- cookieSessionId := http.Cookie{
- Name: "smarthomedSessionId",
- Value: sessionIdString,
- Path: "/",
- MaxAge: 10000,
- Expires: expire,
- HttpOnly: false,
- }
- cookieHouseName := http.Cookie{
- Name: "smarthomedHouseName",
- Value: houseName,
- Path:"/",
- MaxAge: 10000,
- Expires: expire,
- HttpOnly: false,
- }
- http.SetCookie(*w, &cookieUserId)
- http.SetCookie(*w, &cookieSessionId)
- http.SetCookie(*w, &cookieHouseName)
- valueString := "'" + userId + "', '" + SessionId.String() + "'"
- if controller.IsEntryInDb("sessions", "WHERE user_id='"+userId+"';") {
- controller.UpdateDbEntry("sessions", "session_id", "'"+SessionId.String()+"'", "WHERE user_id='"+userId+"';")
- } else {
- if controller.InsertNewDbEntry("sessions", "user_id, session_id", valueString) {
- fmt.Println("session created.")
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement