Advertisement
Guest User

Sample Function Login Go API (Auth Laravel-like)

a guest
Nov 15th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 4.64 KB | None | 0 0
  1. func Login(db *gorm.DB) http.HandlerFunc {
  2.     return func(w http.ResponseWriter, r *http.Request) {
  3.         w.Header().Set("Content-Type", "application/json")
  4.  
  5.         result := models.Encrypt{}
  6.         user := models.Usr_{}
  7.         user_profile := models.UserProfile{}
  8.         avatar_url := "http://xadminx.shortir.com/avatar/small/"
  9.         login_resp := models.LoginResp{}
  10.  
  11.         db.Table("usr_").Where("email = ?", r.FormValue("email")).Scan(&user)
  12.  
  13.         if user.Id > 0 {
  14.             // create iv and key
  15.             key, _ := base64.StdEncoding.DecodeString(settings.APP_KEY)
  16.             result.Iv = make([]byte, 16)
  17.             rand.Read(result.Iv)
  18.  
  19.             crypter, _ := utils.NewCrypter(key, result.Iv)
  20.  
  21.             // encoded, _ := crypter.Encrypt([]byte("adgjmptw"))
  22.             // result.Value = base64.StdEncoding.EncodeToString(encoded)
  23.  
  24.             // Iv := base64.StdEncoding.EncodeToString(result.Iv)
  25.             // result.Mac = hash(encoded, []byte(Iv),key)
  26.  
  27.             // jsons, _ := json.Marshal(result)
  28.  
  29.             // encrypted := base64.StdEncoding.EncodeToString(jsons)
  30.  
  31.             decoded, _ := crypter.Decrypt([]byte(user.Password))
  32.             s := strings.Split(string(decoded), ":\"")
  33.  
  34.             if user.Status == 1 {
  35.                 if len(s) > 1 {
  36.                     s[1] = strings.TrimRight(s[1], "\";")
  37.                     password := s[1]
  38.                     hasher := md5.New()
  39.                     hasher.Write([]byte(r.FormValue("password")))
  40.  
  41.                     if password == r.FormValue("password") || user.Password == hex.EncodeToString(hasher.Sum(nil)) {
  42.                         db.Table("usr_").Select(`
  43.                             usr_.id as id_usr,
  44.                             usr_.full_name,
  45.                             usr_.email,
  46.                             usr_.phone,
  47.                             usr_.banned,
  48.                             usr_.log,
  49.                             usr_.latest_post,
  50.                             usr_.push_notif,
  51.                             usr_profile.location,
  52.                             usr_profile.quotes,
  53.                             usr_profile.website,
  54.                             usr_profile.gender,
  55.                             CONCAT( '`+avatar_url+`' , usr_profile.avatar) as avatar
  56.                         `).Where("usr_.id = ?", user.Id).Joins(`
  57.                             JOIN usr_profile ON usr_profile.id_usr = usr_.id
  58.                         `).Order("usr_.latest_post DESC").First(&user_profile)
  59.  
  60.                         // update token
  61.                         newToken := md5.New()
  62.                         newToken.Write([]byte(time.Now().String()))
  63.  
  64.                         db.Model(&user).Update("token", hex.EncodeToString(newToken.Sum(nil)))
  65.  
  66.                         // empty password
  67.                         user.Password = ""
  68.                         user.Passview = ""
  69.  
  70.                         login_resp.Message = "Login sukses!"
  71.                         login_resp.Status = true
  72.                         login_resp.Data = &user
  73.                         login_resp.Profile = &user_profile
  74.  
  75.                         outgoingJSON, _ := json.Marshal(login_resp)
  76.                         fmt.Fprint(w, string(outgoingJSON))
  77.                     } else {
  78.                         login_resp.Message = "Login gagal!"
  79.                         login_resp.Status = false
  80.  
  81.                         outgoingJSON, _ := json.Marshal(login_resp)
  82.                         fmt.Fprint(w, string(outgoingJSON))
  83.                     }
  84.                 } else {
  85.                     password := string(decoded)
  86.  
  87.                     hasher := md5.New()
  88.                     hasher.Write([]byte(r.FormValue("password")))
  89.  
  90.                     if password == r.FormValue("password") || user.Password == hex.EncodeToString(hasher.Sum(nil)) {
  91.                         db.Table("usr_").Select(`
  92.                             usr_.id as id_usr,
  93.                             usr_.full_name,
  94.                             usr_.email,
  95.                             usr_.phone,
  96.                             usr_.banned,
  97.                             usr_.log,
  98.                             usr_.latest_post,
  99.                             usr_.push_notif,
  100.                             usr_profile.location,
  101.                             usr_profile.quotes,
  102.                             usr_profile.website,
  103.                             usr_profile.gender,
  104.                             CONCAT( '`+avatar_url+`' , usr_profile.avatar) as avatar
  105.                         `).Where("usr_.id = ?", user.Id).Joins(`
  106.                             JOIN usr_profile ON usr_profile.id_usr = usr_.id
  107.                         `).Order("usr_.latest_post DESC").First(&user_profile)
  108.  
  109.                         // update token
  110.                         newToken := md5.New()
  111.                         newToken.Write([]byte(time.Now().String()))
  112.  
  113.                         db.Model(&user).Update("token", hex.EncodeToString(newToken.Sum(nil)))
  114.  
  115.                         // empty password
  116.                         user.Password = ""
  117.                         user.Passview = ""
  118.  
  119.                         login_resp.Message = "Login sukses!"
  120.                         login_resp.Status = true
  121.                         login_resp.Data = &user
  122.                         login_resp.Profile = &user_profile
  123.  
  124.                         outgoingJSON, _ := json.Marshal(login_resp)
  125.                         fmt.Fprint(w, string(outgoingJSON))
  126.                     } else {
  127.                         login_resp.Message = "Login gagal!"
  128.                         login_resp.Status = false
  129.  
  130.                         outgoingJSON, _ := json.Marshal(login_resp)
  131.                         fmt.Fprint(w, string(outgoingJSON))
  132.                     }
  133.                 }
  134.             } else {
  135.                 login_resp.Message = "Akun belum diaktivasi!"
  136.                 login_resp.Status = false
  137.  
  138.                 outgoingJSON, _ := json.Marshal(login_resp)
  139.                 fmt.Fprint(w, string(outgoingJSON))
  140.             }
  141.         } else {
  142.             login_resp.Message = "Login gagal!"
  143.             login_resp.Status = false
  144.  
  145.             outgoingJSON, _ := json.Marshal(login_resp)
  146.             fmt.Fprint(w, string(outgoingJSON))
  147.         }
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement