Browse Source

Fix redirect bug during signup

develop
Tovi Jaeschke-Rogers 2 years ago
parent
commit
33748cbf08
2 changed files with 68 additions and 13 deletions
  1. +49
    -3
      Backend/Api/Auth/Signup.go
  2. +19
    -10
      mobile/lib/views/authentication/signup.dart

+ 49
- 3
Backend/Api/Auth/Signup.go View File

@ -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)
}

+ 19
- 10
mobile/lib/views/authentication/signup.dart View File

@ -1,6 +1,7 @@
import 'dart:convert';
import 'dart:typed_data';
import 'package:Envelope/utils/storage/session_cookie.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
@ -167,13 +168,14 @@ class _SignupWidgetState extends State<SignupWidget> {
return;
}
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Processing Data')),
);
signUp()
.then((dynamic) {
Navigator.of(context).popUntil((route) => route.isFirst);
Navigator.
pushNamedAndRemoveUntil(
context,
'/home',
ModalRoute.withName('/home'),
);
}).catchError((error) {
showMessage('Failed to signup to Envelope, please try again later', context);
});
@ -266,7 +268,7 @@ class _SignupWidgetState extends State<SignupWidget> {
);
}
Future<SignupResponse> signUp() async {
Future<dynamic> signUp() async {
await MyProfile.setServerUrl(_serverUrlController.text);
var keyPair = CryptoUtils.generateRSAKeyPair();
@ -293,12 +295,19 @@ class _SignupWidgetState extends State<SignupWidget> {
}),
);
SignupResponse response = SignupResponse.fromJson(jsonDecode(resp.body));
if (resp.statusCode != 200) {
throw Exception('Unable to signup to envelope');
}
if (resp.statusCode != 201) {
throw Exception(response.message);
String? rawCookie = resp.headers['set-cookie'];
if (rawCookie != null) {
int index = rawCookie.indexOf(';');
setSessionCookie((index == -1) ? rawCookie : rawCookie.substring(0, index));
}
return response;
return await MyProfile.login(
json.decode(resp.body),
_passwordController.text,
);
}
}

Loading…
Cancel
Save