From 33748cbf085150960a673907be5b35d0c95b0a9b Mon Sep 17 00:00:00 2001 From: Tovi Jaeschke-Rogers Date: Sat, 5 Nov 2022 16:37:07 +1030 Subject: [PATCH] Fix redirect bug during signup --- Backend/Api/Auth/Signup.go | 52 +++++++++++++++++++-- mobile/lib/views/authentication/signup.dart | 29 ++++++++---- 2 files changed, 68 insertions(+), 13 deletions(-) diff --git a/Backend/Api/Auth/Signup.go b/Backend/Api/Auth/Signup.go index 0e9e80e..433b144 100644 --- a/Backend/Api/Auth/Signup.go +++ b/Backend/Api/Auth/Signup.go @@ -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) } diff --git a/mobile/lib/views/authentication/signup.dart b/mobile/lib/views/authentication/signup.dart index 5739101..4d393b8 100644 --- a/mobile/lib/views/authentication/signup.dart +++ b/mobile/lib/views/authentication/signup.dart @@ -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 { 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 { ); } - Future signUp() async { + Future signUp() async { await MyProfile.setServerUrl(_serverUrlController.text); var keyPair = CryptoUtils.generateRSAKeyPair(); @@ -293,12 +295,19 @@ class _SignupWidgetState extends State { }), ); - 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, + ); } }