go-roblox/session.go

46 lines
957 B
Go
Raw Normal View History

2017-01-07 05:20:45 +00:00
package roblox
import (
"fmt"
"net/http"
"net/http/cookiejar"
"net/url"
)
// Session struct for roblox login session data and members
type Session struct {
2017-09-28 23:27:53 +00:00
ID int
2017-01-07 08:48:57 +00:00
Username string
2017-01-07 05:20:45 +00:00
client *http.Client
}
// New create a new session and logs in with provided data
2017-09-28 23:27:53 +00:00
func New(username, password string) (*Session, error) {
2017-01-07 05:20:45 +00:00
cookieJar, _ := cookiejar.New(nil)
client := &http.Client{
Jar: cookieJar,
}
v := url.Values{}
2017-09-28 23:27:53 +00:00
v.Set("username", username)
v.Set("password", password)
2017-01-07 05:20:45 +00:00
v.Set("submitLogin", "Log In")
v.Set("ReturnUrl", "")
2017-09-28 23:27:53 +00:00
session := Session{0, username, client}
2017-01-07 05:20:45 +00:00
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)
}
2017-09-28 23:27:53 +00:00
info, err := session.GetUserInfo()
if err != nil {
return nil, fmt.Errorf("Failed to retrieve user information: %s", err)
}
session.ID = info.UserID
2017-01-07 05:20:45 +00:00
return &session, err
}