Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Google driving #44

Merged
merged 21 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading