diff --git a/EduSync.exe b/EduSync.exe index 74432d6..c0bbb59 100644 Binary files a/EduSync.exe and b/EduSync.exe differ diff --git a/adminHandler.go b/adminHandler.go index 421bc3c..49e6ee0 100644 --- a/adminHandler.go +++ b/adminHandler.go @@ -45,7 +45,7 @@ func AdminHandler(router *mux.Router) { currentUser, err := GetCurrentUser(req) if err != nil { - http.Error(res, "Unauthorized", http.StatusUnauthorized) + http.Error(res, err.Error(), http.StatusInternalServerError) return } @@ -69,7 +69,7 @@ func AdminHandler(router *mux.Router) { currentUser, err := GetCurrentUser(req) if err != nil { - http.Error(res, "Unauthorized", http.StatusUnauthorized) + http.Error(res, err.Error(), http.StatusInternalServerError) return } diff --git a/authHandler.go b/authHandler.go index 9a9cbbb..9a32c94 100644 --- a/authHandler.go +++ b/authHandler.go @@ -18,11 +18,20 @@ func AuthHandler(router *mux.Router, config *Config) { maxAge := 86400 * 30 // 30 days isProd := true // Set to true when serving over https - store := sessions.NewCookieStore([]byte(config.SessionSecret)) + store = sessions.NewCookieStore( + []byte(config.AuthKey), + []byte(config.EncryptKey), + ) store.MaxAge(maxAge) store.Options.Path = "/" store.Options.HttpOnly = true // HttpOnly should always be enabled store.Options.Secure = isProd + store.Options = &sessions.Options{ + Path: "/", + MaxAge: 3600, // 1 hour + HttpOnly: true, + Secure: true, // This should be true if your application is served over HTTPS + } gothic.Store = store goth.UseProviders(google.New(config.GoogleClientID, config.GoogleClientSecret, "https://localhost:8080/auth/google/callback", "email", "profile")) @@ -43,36 +52,38 @@ func AuthHandler(router *mux.Router, config *Config) { // Only store the user object into the session if userRole is not an empty string if userRole != "" { - // Create a User object with the user role - currentUser := User{ - GoogleID: user.UserID, - Name: user.Name, - Email: user.Email, - ContactNumber: userObj.ContactNumber, // Use contact number from the retrieved user object - Role: userObj.Role, - CreatedAt: userObj.CreatedAt, - UpdatedAt: userObj.UpdatedAt, - } + // // Create a User object with the user role + // currentUser := User{ + // GoogleID: user.UserID, + // Name: user.Name, + // Email: user.Email, + // ContactNumber: userObj.ContactNumber, // Use contact number from the retrieved user object + // Role: userObj.Role, + // CreatedAt: userObj.CreatedAt, + // UpdatedAt: userObj.UpdatedAt, + // } - // Serialize the user object to JSON - userData, err := json.Marshal(currentUser) - if err != nil { - http.Error(res, err.Error(), http.StatusInternalServerError) - return - } + // // Serialize the user object to JSON + // userData, err := json.Marshal(currentUser) + // if err != nil { + // http.Error(res, err.Error(), http.StatusInternalServerError) + // return + // } - // Get the session and store the user data - session, err := store.Get(req, "auth-session") - if err != nil { - http.Error(res, err.Error(), http.StatusInternalServerError) - return - } - session.Values["user"] = userData - err = session.Save(req, res) - if err != nil { - http.Error(res, err.Error(), http.StatusInternalServerError) - return - } + // // Get the session and store the user data + // session, err := store.Get(req, "auth-session") + // if err != nil { + // http.Error(res, err.Error(), http.StatusInternalServerError) + // return + // } + // session.Values["user"] = userData + // err = session.Save(req, res) + // if err != nil { + // http.Error(res, err.Error(), http.StatusInternalServerError) + // return + // } + + SetCurrentUser(res, req, userObj) // Redirect based on user role if userRole == "Admin" { @@ -106,3 +117,23 @@ func AuthHandler(router *mux.Router, config *Config) { t.Execute(res, false) }).Methods("GET") } + +func SetCurrentUser(res http.ResponseWriter, req *http.Request, user User) error { + session, err := store.Get(req, "auth-session") + if err != nil { + return fmt.Errorf("error retrieving session: %v", err) + } + + userData, err := json.Marshal(user) + if err != nil { + return fmt.Errorf("error marshalling user data: %v", err) + } + + session.Values["user"] = userData + err = session.Save(req, res) + if err != nil { + return fmt.Errorf("error saving session: %v", err) + } + + return nil +} diff --git a/config.go b/config.go index 5a65034..71c42ff 100644 --- a/config.go +++ b/config.go @@ -11,6 +11,8 @@ type Config struct { GoogleClientID string `json:"google_client_id"` GoogleClientSecret string `json:"google_client_secret"` SessionSecret string `json:"session_secret"` + AuthKey string `json:"auth_key"` + EncryptKey string `json:"encrypt_key"` } // LoadConfig reads the configuration from a file diff --git a/database.go b/database.go index 46a0f4d..5d71f0e 100644 --- a/database.go +++ b/database.go @@ -30,7 +30,7 @@ func initDB(app *firebase.App) error { func GetCurrentUser(req *http.Request) (User, error) { session, err := store.Get(req, "auth-session") if err != nil { - return User{}, err + return User{}, fmt.Errorf("error retrieving session: %v", err) } userData, ok := session.Values["user"].([]byte) @@ -41,7 +41,7 @@ func GetCurrentUser(req *http.Request) (User, error) { var user User err = json.Unmarshal(userData, &user) if err != nil { - return User{}, err + return User{}, fmt.Errorf("error unmarshalling user data: %v", err) } return user, nil diff --git a/database_test.go b/database_test.go index 84ff395..38944d6 100644 --- a/database_test.go +++ b/database_test.go @@ -13,7 +13,7 @@ var students = []Student{ User: User{ GoogleID: "test-student", Name: "John Doe", - Email: "jeyvianangjieen@gmail.com", + Email: "jeyvianang112462@gmail.com", ContactNumber: "91234567", Role: "Student", }, @@ -43,7 +43,7 @@ var admin = Admin{ GoogleID: "test-admin", Name: "Awesomeness", ContactNumber: "99999999", - Email: "awesome_admin@nk.com", + Email: "jeyvianangjieen@gmail.com", Role: "Admin", }, BasePay: 15, diff --git a/firebase.go b/firebase.go index 409c9f1..2a1ee59 100644 --- a/firebase.go +++ b/firebase.go @@ -6,7 +6,7 @@ import ( "log" "os" - //"github.com/joho/godotenv" + "github.com/joho/godotenv" firebase "firebase.google.com/go" "google.golang.org/api/option" @@ -14,33 +14,33 @@ import ( // Use godot package to load/read the .env file and // return the value of the key (for local env) -// func goDotEnvVariable(key string) string { +func goDotEnvVariable(key string) string { -// // load .env file -// err := godotenv.Load(".env") + // load .env file + err := godotenv.Load(".env") -// if err != nil { -// log.Fatalf("Error loading .env file") -// } + if err != nil { + log.Fatalf("Error loading .env file") + } -// return os.Getenv(key) -// } + return os.Getenv(key) +} // InitializeFirebase initializes the Firebase app and sets the global firebaseClient variable func initializeFirebase() error { ctx := context.Background() - databaseURL, found := os.LookupEnv("DATABASE_URL") - if !found { - log.Fatalf("DATABASE_URL is not set in the environment variables") - } - opt := option.WithCredentialsFile("edusync-7bd5e-firebase-adminsdk-x49uh-af084a6314.json") - - // databaseURL := goDotEnvVariable("DATABASE_URL") - // if databaseURL == "" { - // return fmt.Errorf("DATABASE_URL is not set in the environment variables") + // databaseURL, found := os.LookupEnv("DATABASE_URL") + // if !found { + // log.Fatalf("DATABASE_URL is not set in the environment variables") // } - // opt := option.WithCredentialsFile("edusync-test-firebase-adminsdk-hk5kl-9af0162b09.json") + // opt := option.WithCredentialsFile("edusync-7bd5e-firebase-adminsdk-x49uh-af084a6314.json") + + databaseURL := goDotEnvVariable("DATABASE_URL") + if databaseURL == "" { + return fmt.Errorf("DATABASE_URL is not set in the environment variables") + } + opt := option.WithCredentialsFile("edusync-test-firebase-adminsdk-hk5kl-9af0162b09.json") conf := &firebase.Config{DatabaseURL: databaseURL} diff --git a/main.go b/main.go index b69f687..80326a7 100644 --- a/main.go +++ b/main.go @@ -32,7 +32,6 @@ func main() { // Set up authentication routes AuthHandler(router, config) MainHandler(router) - AdminHandler(router) log.Println("listening on localhost:8080") err = http.ListenAndServeTLS(":8080", "cert.pem", "key.pem", router) diff --git a/templates/admin/edit_student.html b/templates/admin/edit_student.html index 0cf80c4..3245c1b 100644 --- a/templates/admin/edit_student.html +++ b/templates/admin/edit_student.html @@ -1,39 +1,41 @@ - EduSync - - - - - - + EduSync + + + + + + -
- + +

Edit Student Details

@@ -120,13 +122,15 @@

Edit Student Details



+
+

-
+

-
+

-
+


@@ -134,16 +138,16 @@

Edit Student Details

-
+
- - - - - - - - + + + + + + + + \ No newline at end of file diff --git a/templates/admin/search_student.html b/templates/admin/search_student.html index 10eaad5..d6cef9d 100644 --- a/templates/admin/search_student.html +++ b/templates/admin/search_student.html @@ -9,6 +9,21 @@