|
|
@ -1,8 +1,10 @@ |
|
|
|
package Auth |
|
|
|
|
|
|
|
import ( |
|
|
|
"database/sql/driver" |
|
|
|
"encoding/json" |
|
|
|
"net/http" |
|
|
|
"time" |
|
|
|
|
|
|
|
"git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" |
|
|
|
) |
|
|
@ -18,8 +20,14 @@ type signup struct { |
|
|
|
// Signup to the platform
|
|
|
|
func Signup(w http.ResponseWriter, r *http.Request) { |
|
|
|
var ( |
|
|
|
user Database.User |
|
|
|
err error |
|
|
|
user Database.User |
|
|
|
expiresAt time.Time |
|
|
|
session Database.Session |
|
|
|
messageExpiryRaw driver.Value |
|
|
|
messageExpiry string |
|
|
|
imageLink string |
|
|
|
returnJSON []byte |
|
|
|
err error |
|
|
|
) |
|
|
|
|
|
|
|
err = json.NewDecoder(r.Body).Decode(&user) |
|
|
@ -61,5 +69,43 @@ func Signup(w http.ResponseWriter, r *http.Request) { |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent) |
|
|
|
// TODO: Revisit before production
|
|
|
|
expiresAt = time.Now().Add(12 * time.Hour) |
|
|
|
|
|
|
|
session = Database.Session{ |
|
|
|
UserID: user.ID, |
|
|
|
Expiry: expiresAt, |
|
|
|
} |
|
|
|
|
|
|
|
err = (&session).CreateSession() |
|
|
|
if err != nil { |
|
|
|
http.Error(w, "Unauthorized", http.StatusUnauthorized) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
http.SetCookie(w, &http.Cookie{ |
|
|
|
Name: "session_token", |
|
|
|
Value: session.ID.String(), |
|
|
|
Expires: expiresAt, |
|
|
|
}) |
|
|
|
|
|
|
|
if user.AttachmentID != nil { |
|
|
|
imageLink = user.Attachment.FilePath |
|
|
|
} |
|
|
|
|
|
|
|
messageExpiryRaw, _ = user.MessageExpiryDefault.Value() |
|
|
|
messageExpiry, _ = messageExpiryRaw.(string) |
|
|
|
|
|
|
|
returnJSON, err = json.MarshalIndent(loginResponse{ |
|
|
|
UserID: user.ID.String(), |
|
|
|
Username: user.Username, |
|
|
|
AsymmetricPublicKey: user.AsymmetricPublicKey, |
|
|
|
AsymmetricPrivateKey: user.AsymmetricPrivateKey, |
|
|
|
SymmetricKey: user.SymmetricKey, |
|
|
|
MessageExpiryDefault: messageExpiry, |
|
|
|
ImageLink: imageLink, |
|
|
|
}, "", " ") |
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK) |
|
|
|
w.Write(returnJSON) |
|
|
|
} |