go-roblox/session.go
2017-09-28 19:27:53 -04:00

46 lines
957 B
Go

package roblox
import (
"fmt"
"net/http"
"net/http/cookiejar"
"net/url"
)
// Session struct for roblox login session data and members
type Session struct {
ID int
Username string
client *http.Client
}
// New create a new session and logs in with provided data
func New(username, password string) (*Session, error) {
cookieJar, _ := cookiejar.New(nil)
client := &http.Client{
Jar: cookieJar,
}
v := url.Values{}
v.Set("username", username)
v.Set("password", password)
v.Set("submitLogin", "Log In")
v.Set("ReturnUrl", "")
session := Session{0, username, client}
resp, err := client.PostForm("https://www.roblox.com/newlogin", v)
if resp.StatusCode != 200 {
return &session, fmt.Errorf("Messaged send failed. Status %d", resp.StatusCode)
}
info, err := session.GetUserInfo()
if err != nil {
return nil, fmt.Errorf("Failed to retrieve user information: %s", err)
}
session.ID = info.UserID
return &session, err
}