-
Notifications
You must be signed in to change notification settings - Fork 1
/
RegisterUser.cs
113 lines (98 loc) · 3.93 KB
/
RegisterUser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using DG.Tweening;
using Newtonsoft.Json;
using UnityEngine;
using UnityEngine.Networking;
using TMPro;
using UnityEngine.UI;
public class RegisterUser : MonoBehaviour
{
[SerializeField] private TMP_InputField nameInputField;
[SerializeField] private TMP_InputField emailInputField;
[SerializeField] private TMP_InputField passwordInputField;
[SerializeField] private TMP_InputField confirmPasswordInputField;
[SerializeField] private Button createAccountButton;
[SerializeField] private Button loginButton;
[SerializeField] private TMP_Text infoText;
private CanvasManager canvasManager;
private void Awake()
{
loginButton.onClick.AddListener(OnClickedLoginButton);
createAccountButton.onClick.AddListener(Register);
canvasManager = FindObjectOfType(typeof(CanvasManager)) as CanvasManager;
}
private void Register()
{
if (nameInputField.text == "" || emailInputField.text == "")
{
infoText.text = "Name and email cannot be empty";
infoText.color = Color.red;
return;
}
if (emailInputField.text.Contains("@") == false || emailInputField.text.Contains(".") == false)
{
infoText.text = "Invalid email address";
infoText.color = Color.red;
return;
}
if ( passwordInputField.text.Length < 6)
{
infoText.text = "Password must be at least 6 characters";
infoText.color = Color.red;
return;
}
if(confirmPasswordInputField.text != passwordInputField.text)
{
infoText.text = "Passwords do not match";
infoText.color = Color.red;
return;
}
string url = AppVariables.HttpServerUrl + "/auth/register";
var registerData = new RegisterRequest();
registerData.username = nameInputField.text;
registerData.email = emailInputField.text;
registerData.password = passwordInputField.text;
string bodyJsonString = JsonConvert.SerializeObject(registerData);
StartCoroutine(Post(url, bodyJsonString));
}
IEnumerator Post(string url, string bodyJsonString)
{
var request = new UnityWebRequest(url, "POST");
byte[] bodyRaw = Encoding.UTF8.GetBytes(bodyJsonString);
request.uploadHandler = (UploadHandler) new UploadHandlerRaw(bodyRaw);
request.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
yield return request.SendWebRequest();
var response = request.downloadHandler.text;
var _useraData = JsonConvert.DeserializeObject<RegisterResponse>(response);
if (request.responseCode != 200 || _useraData?.username == null)
{
infoText.text = "Error: ";
infoText.color = Color.red;
}
else
{
infoText.text = "Successfully registered";
infoText.color = Color.green;
// Burada token alıp kaydetmek lazım ki login olmuş gibi olsun
// PersistenceManager.UserToken = _useraData.token;
PersistenceManager.UserName = _useraData.username;
// 2 saniye sonra login sayfasına geç
DOVirtual.DelayedCall(2f, () =>
{
canvasManager.ShowLogInPage();
canvasManager.HideSignUpPage();
});
}
request.downloadHandler.Dispose();
request.uploadHandler.Dispose();
}
private void OnClickedLoginButton()
{
canvasManager.ShowLogInPage();
canvasManager.HideSignUpPage();
}
}