mirror of
https://github.com/ssep1ol/overleaf-registration-be.git
synced 2026-07-21 20:40:21 +00:00
First commit
This commit is contained in:
42
overleaf.go
42
overleaf.go
@@ -46,29 +46,29 @@ func (o *Overleaf) get(endpoint string) (*http.Response, error) {
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// post performs a POST request
|
||||
func (o *Overleaf) post(endpoint string, data map[string]string) (*http.Response, error) {
|
||||
if o.CSRF == "" {
|
||||
if err := o.obtainCSRF(); err != nil {
|
||||
return nil, fmt.Errorf("failed to obtain CSRF token: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
fullURL := fmt.Sprintf("%s%s", o.BaseURL, endpoint)
|
||||
url := fmt.Sprintf("%s%s", o.BaseURL, endpoint)
|
||||
data["_csrf"] = o.CSRF
|
||||
formData := encodeFormData(data)
|
||||
|
||||
req, err := http.NewRequest("POST", fullURL, bytes.NewBufferString(formData))
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBufferString(formData))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create POST request: %w", err)
|
||||
return nil, fmt.Errorf("failed to create POST request: %v", err)
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
|
||||
resp, err := o.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("POST request to %s failed: %w", fullURL, err)
|
||||
// Log cookies for debugging
|
||||
cookies := o.Client.Jar.Cookies(req.URL)
|
||||
for _, cookie := range cookies {
|
||||
log.Printf("Cookie sent: %s=%s", cookie.Name, cookie.Value)
|
||||
}
|
||||
return resp, nil
|
||||
|
||||
// Include cookies in the request
|
||||
for _, cookie := range cookies {
|
||||
req.AddCookie(cookie)
|
||||
}
|
||||
|
||||
return o.Client.Do(req)
|
||||
}
|
||||
|
||||
func (o *Overleaf) obtainCSRF() error {
|
||||
@@ -125,20 +125,28 @@ func (o *Overleaf) Login(email, password string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// RegisterUser registers a new user
|
||||
func (o *Overleaf) RegisterUser(email string) error {
|
||||
log.Printf("Attempting to register user with email: %s", email)
|
||||
log.Printf("Using CSRF token: %s", o.CSRF)
|
||||
|
||||
resp, err := o.post("/admin/register", map[string]string{
|
||||
"email": email,
|
||||
"_csrf": o.CSRF, // Include the CSRF token
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to register user: %w", err)
|
||||
return fmt.Errorf("failed to send register request: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
// Log response for debugging
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
log.Printf("Register user response: %s", string(body))
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("register user returned status %d: %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
log.Println("User registered successfully.")
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user