Преглед изворни кода

Hashade lösenord, uppdaterad inloggning

Axel Nordh пре 7 година
родитељ
комит
489618d5ce
3 измењених фајлова са 21 додато и 9 уклоњено
  1. 19 7
      Assets/Scripts/Login/Login.cs
  2. 1 1
      dbFiles/Login.php
  3. 1 1
      dbFiles/Register.php

+ 19 - 7
Assets/Scripts/Login/Login.cs

@@ -6,6 +6,8 @@ using UnityEngine.UI;
 using UnityEngine.EventSystems;
 using UnityEngine.SceneManagement;
 using System;
+using System.Security.Cryptography;
+using System.Text;
 
 public class Login : MonoBehaviour {
 
@@ -88,14 +90,24 @@ public class Login : MonoBehaviour {
         User u = new User();
         JsonUtility.FromJsonOverwrite(result, u);
 
-        if (int.TryParse(www.downloadHandler.text, out int userId)) { 
-            errorColor.a = 0;
-            errorText.color = errorColor;
-            PlayerPrefs.SetInt("UserId", userId);
-            // load next scene
-            SceneManager.LoadScene("MainMenu");
+        if (!u.userId.Equals("")) {
+            byte[] pwd = Encoding.UTF8.GetBytes(u.salt + Password);
+            SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
+            string pass = sha1.ComputeHash(pwd).ToString();
+            if (pass.Equals(u.pass)) {
+                errorColor.a = 0;
+                errorText.color = errorColor;
+                Int32.TryParse(u.userId, out int userId);
+                PlayerPrefs.SetInt("UserId", userId);
+                // load next scene
+                SceneManager.LoadScene("MainMenu");
+            } else {
+                errorText.text = "Felaktig användare/lösenord";
+                errorColor.a = 1;
+                errorText.color = errorColor;
+            }
         } else {
-            errorText.text = "Misslyckades med inloggning";
+            errorText.text = "Användaren hittades inte";
             errorColor.a = 1;
             errorText.color = errorColor;
         }

+ 1 - 1
dbFiles/Login.php

@@ -21,7 +21,7 @@
 	$result = $conn->query($sql);
 	if ($result->num_rows == 1) {
 		$data = $result->fetch_assoc();
-		$updateLastLoginSql = "UPDATE users SET last_login = " . NOW() . " WHERE id = " . $data['id'];
+		$updateLastLoginSql = "UPDATE users SET last_login = NOW() WHERE id = " . $data['id'];
 		$conn->query($updateLastLoginSql);
 		$returnArray = array('userId' => $data['id'], 'pass' => $data['password'], 'salt' => $data['salt']);
 		echo json_encode($returnArray);

+ 1 - 1
dbFiles/Register.php

@@ -20,7 +20,7 @@
 		return;
 	}
 	
-	$sql = "INSERT INTO users (username, password, email, salt, last_login) VALUES ('$newUsername', '$newPassword', '$newEmail', '$salt', " . NOW() . ")";
+	$sql = "INSERT INTO users (username, password, email, salt, last_login) VALUES ('$newUsername', '$newPassword', '$newEmail', '$salt', NOW())";
 	if ($conn->query($sql) === true) {
 		echo $conn->insert_id;
 	} else {