func AddComment(c *gin.Context) { var prof_in *Profile status := http.StatusOK if auth.IsAuthenticated(c) { conn := store.New(c) var comment Comment c.BindJSON(&comment) prof_id, _ := strconv.ParseInt(comment.Prof_Id, 10, 64) det_id, _ := strconv.ParseInt(comment.Det_Id, 10, 64) det_key := conn.DatastoreKeyWithKind("ProfileDetails", det_id) prof_in = &Profile{Id: prof_id, Parent: det_key} conn.Get(prof_in) comment.Id = uuid.NewV4().String() comment.Time = time.Now().Format("2006-01-02 15-04-05") comment.Author = auth.GetUser(c).Name prof_in.Comments = append(prof_in.Comments, comment) conn.Add(prof_in) status = http.StatusOK } else { auth.SessionSave(c) auth.Login(c) } c.JSON(status, prof_in.Comments) }
func AddLikes(c *gin.Context) { status := http.StatusOK var likes int64 authenticated := "false" if auth.IsAuthenticated(c) { authenticated = "true" conn := store.New(c) prof_id, _ := strconv.ParseInt(c.Param("prof_id"), 10, 64) det_id, _ := strconv.ParseInt(c.Param("det_id"), 10, 64) det_key := conn.DatastoreKeyWithKind("ProfileDetails", det_id) prof_in := &Profile{Id: prof_id, Parent: det_key} conn.Get(prof_in) user := auth.GetUser(c) like_type := c.Query("type") like_id := c.Query("like_id") var incr = 1 if auth.IsLiked(c, like_id) { incr = -1 sort.Strings(user.Likes) i := sort.SearchStrings(user.Likes, like_id) user.Likes = append(user.Likes[:i], user.Likes[i+1:]...) } else { user.Likes = append(user.Likes, like_id) } conn.Goon.RunInTransaction(func(Goon *goon.Goon) error { Goon.Put(&user) return nil }, nil) if like_type == "profile" { prof_in.Likes += int64(incr) likes = prof_in.Likes } if like_type == "comment" { for i, comm := range prof_in.Comments { if comm.Id == like_id { prof_in.Comments[i].Likes += int64(incr) likes = prof_in.Comments[i].Likes } } } if like_type == "survey" { for i, surv := range prof_in.Surveys { if surv.Id == like_id { prof_in.Surveys[i].Likes += int64(incr) likes = prof_in.Surveys[i].Likes } } } conn.Add(prof_in) authenticated = "true" status = http.StatusOK } c.JSON(status, gin.H{"authenticated": authenticated, "likes": likes}) }
func Admin(c *gin.Context) { if auth.IsAuthenticated(c) { c.HTML(http.StatusOK, "admin.html", gin.H{}) } else { auth.Login(c) } }
func FBLogin(c *gin.Context) { // grab the code fragment aecontext := appengine.NewContext(c.Request) code := c.Query("code") fbConfig := FBConfig() token, err := fbConfig.Exchange(aecontext, code) if err != nil { log.Errorf(aecontext, err.Error()) } client := urlfetch.Client(aecontext) response, err := client.Get("https://graph.facebook.com/v2.5/me?fields=id,name,email&access_token=" + token.AccessToken) log.Debugf(aecontext, fmt.Sprintf("token.AccessToken %#v", token.AccessToken)) // handle err. You need to change this into something more robust // such as redirect back to home page with error message if err != nil { c.String(http.StatusOK, "Error") } var fb_user FB_User log.Debugf(aecontext, fmt.Sprintf("fb_response %#v", response)) Bind(response, &fb_user) log.Debugf(aecontext, fmt.Sprintf("token %#v", token)) user := &auth.User{ Email: fb_user.Email, Name: fb_user.Username, Token: *token, LoginType: "facebook", } var redir_url = "" if service.VerifyVolunteerRequest(c, user) { redir_url = "/volsuccess" } log.Debugf(aecontext, fmt.Sprintf("user %#v", user)) if !auth.IsAuthenticated(c) { auth.CreateSession(c, user) } if redir_url == "" { auth.Redirect(c, redir_url) } else { auth.Redirect(c) } }
func SendVolunteerRequest(c *gin.Context) { if auth.IsAuthenticated(c) { user := &auth.User{} conn := store.New(c) c.BindJSON(user) user.Temp_Req_Id = uuid.NewV4().String() user.Role = "pending" user.Ref_Email = auth.GetUser(c).Email conn.Add(user) SendEmailVolunteer(c, user) c.JSON(http.StatusOK, gin.H{"msg": "send email to volunteer"}) } else { c.JSON(http.StatusOK, gin.H{"msg": "auth required"}) } }
func RefreshFacebookTokens(c *gin.Context) { if auth.IsAuthenticated(c) { user := auth.GetUser(c) if user.LoginType == "facebook" { aecontext := appengine.NewContext(c.Request) conf := FBConfig() toksource := conf.TokenSource(aecontext, &user.Token) sourceToken := oauth2.ReuseTokenSource(&user.Token, toksource) client := oauth2.NewClient(aecontext, sourceToken) client.Get("...") t, _ := sourceToken.Token() user.Token = *t auth.UpdateUser(c, &user) log.Debugf(aecontext, fmt.Sprintf("refresh tokens %#v", t)) } } }
func GoogleLogin(c *gin.Context) { // grab the code fragment aecontext := appengine.NewContext(c.Request) code := c.Query("code") googleconf := GoogleConfig() token, err := googleconf.Exchange(aecontext, code) if err != nil { log.Errorf(aecontext, err.Error()) } client := urlfetch.Client(aecontext) response, err := client.Get("https://www.googleapis.com/userinfo/v2/me?access_token=" + token.AccessToken) log.Debugf(aecontext, fmt.Sprintf("token.AccessToken %#v", token.AccessToken)) // handle err. You need to change this into something more robust // such as redirect back to home page with error message if err != nil { c.String(http.StatusOK, "Error") } var goog_user GoogleUser log.Debugf(aecontext, fmt.Sprintf("fb_response %#v", response)) Bind(response, &goog_user) log.Debugf(aecontext, fmt.Sprintf("fb_response %#v", goog_user)) user := &auth.User{ Email: goog_user.Email, Name: goog_user.Username, Picture: goog_user.PictureUrl, } log.Debugf(aecontext, fmt.Sprintf("user %#v", user)) if !auth.IsAuthenticated(c) { auth.CreateSession(c, user) } auth.Redirect(c) }