Skip to content

Commit

Permalink
Google driving (#44)
Browse files Browse the repository at this point in the history
* uploading is done, but for files only

* added folder id

* testing only

* google drive for student works

* instructors can see classes and access google drive for each class

* removed some comments and handlers from main function

* instructor can upload files, interface done. now need to do ui.

* finished profile for each role, only admin able to edit own profile, left ui

* removed session secret in config

* did ui for login and unregistered

* done html and ui for different role from landing page

* finished ui for user profile, added logout

* finished learning materials ui, left media

* FINALLY DONE OMG

* lalala

* lalala

* added google drive link to parent index

* minor chage

* hooray

* for merging
  • Loading branch information
PeanutBrrutter authored Aug 12, 2024
1 parent 55ee724 commit dcfb49f
Show file tree
Hide file tree
Showing 48 changed files with 3,448 additions and 503 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
edusync-7bd5e-firebase-adminsdk-x49uh-af084a6314.json
edusync-test-firebase-adminsdk-hk5kl-9af0162b09.json
.env
*.json
config.json
edusync-426009-343696fa49b1.json
*.exe
Binary file modified EduSync.exe
Binary file not shown.
49 changes: 48 additions & 1 deletion adminHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ func AdminHandler(router *mux.Router) {
t.Execute(res, nil)
}).Methods("GET")

router.HandleFunc("/admin/profile", func(res http.ResponseWriter, req *http.Request) {
t, err := template.ParseFiles("templates/admin/profile.html")
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
t.Execute(res, nil)
}).Methods("GET")

router.HandleFunc("/admin/profile", func(res http.ResponseWriter, req *http.Request) {
t, err := template.ParseFiles("templates/admin/profile.html")
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
t.Execute(res, nil)
}).Methods("GET")

//searve the search student page
router.HandleFunc("/admin/search_student", func(res http.ResponseWriter, req *http.Request) {
t, err := template.ParseFiles("templates/admin/search_student.html")
Expand Down Expand Up @@ -428,7 +446,7 @@ func AdminHandler(router *mux.Router) {
}).Methods("GET", "PUT")

// Create a new announcement
router.HandleFunc("/admin/announcement/", func(res http.ResponseWriter, req *http.Request) {
router.HandleFunc("/admin/announcement", func(res http.ResponseWriter, req *http.Request) {
switch req.Method {
case http.MethodPost:
var announcement Announcement
Expand All @@ -454,4 +472,33 @@ func AdminHandler(router *mux.Router) {
// POST /admin/announcement
// Request Body: JSON object with announcement details
// Response: HTTP Status Created (201)

router.HandleFunc("/admin/api/profile", func(res http.ResponseWriter, req *http.Request) {
currentUser, err := GetCurrentUser(req)
if err != nil {
http.Error(res, "Unauthorized", http.StatusUnauthorized)
return
}
switch req.Method {
case http.MethodGet:
admin, err := readAdmin(currentUser.GoogleID, req)
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
res.Header().Set("Content-Type", "application/json")
json.NewEncoder(res).Encode(admin)
case http.MethodPut:
var updates map[string]interface{}
if err := json.NewDecoder(req.Body).Decode(&updates); err != nil {
http.Error(res, err.Error(), http.StatusBadRequest)
return
}
if err := updateAdmin(currentUser.GoogleID, updates, req); err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
res.WriteHeader(http.StatusNoContent)
}
}).Methods("GET", "PUT")
}
50 changes: 31 additions & 19 deletions authHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
)

func AuthHandler(router *mux.Router, config *Config) {
maxAge := 86400 * 30 // 30 days
isProd := true // Set to true when serving over https
maxAge := 3600 // 1 hour
isProd := true // Set to true when serving over https

store = sessions.NewCookieStore(
[]byte(config.AuthKey),
Expand All @@ -26,16 +26,23 @@ func AuthHandler(router *mux.Router, config *Config) {
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", "https://www.googleapis.com/auth/drive.file"))

router.HandleFunc("/login", func(res http.ResponseWriter, req *http.Request) {
t, err := template.ParseFiles("templates/login.html")
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
t.Execute(res, false)
}).Methods("GET")

router.HandleFunc("/auth/{provider}", func(res http.ResponseWriter, req *http.Request) {
gothic.BeginAuthHandler(res, req)
}).Methods("GET")

router.HandleFunc("/auth/{provider}/callback", func(res http.ResponseWriter, req *http.Request) {
user, err := gothic.CompleteUserAuth(res, req)
if err != nil {
Expand Down Expand Up @@ -74,17 +81,22 @@ func AuthHandler(router *mux.Router, config *Config) {
}
}).Methods("GET")

router.HandleFunc("/auth/{provider}", func(res http.ResponseWriter, req *http.Request) {
gothic.BeginAuthHandler(res, req)
}).Methods("GET")

router.HandleFunc("/login", func(res http.ResponseWriter, req *http.Request) {
t, err := template.ParseFiles("templates/login.html")
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
t.Execute(res, false)
router.HandleFunc("/logout", func(res http.ResponseWriter, req *http.Request) {
// Clear the session or cookie
http.SetCookie(res, &http.Cookie{
Name: "session_token",
Value: "",
Path: "/",
MaxAge: -1, // This will delete the cookie
})

// Set headers to prevent caching
res.Header().Set("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0")
res.Header().Set("Cache-Control", "post-check=0, pre-check=0")
res.Header().Set("Pragma", "no-cache")

// Redirect to the login page or home page
http.Redirect(res, req, "/", http.StatusFound) // 302 Found
}).Methods("GET")
}

Expand Down
1 change: 0 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
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"`
}
Expand Down
Loading

0 comments on commit dcfb49f

Please sign in to comment.