Skip to content

Commit

Permalink
fixed login
Browse files Browse the repository at this point in the history
  • Loading branch information
ewang001 committed Nov 27, 2023
1 parent 436172c commit beb4350
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 40 deletions.
2 changes: 1 addition & 1 deletion app/newHere1/newHere/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct ContentView: View {
HomePageView()
.environmentObject(locationDataManager)
} else {
LoginView(isAuthenticated: $isAuthenticated, user_id: $userId)
LoginView(isAuthenticated: $isAuthenticated)
}
}
}
Expand Down
3 changes: 0 additions & 3 deletions app/newHere1/newHere/CustomARViewRepresentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ import SwiftUI
import ARKit

struct CustomARViewRepresentable: UIViewRepresentable {
// Binding for user ID
@Binding var userId: String

// Environment objects for message and fetched messages states
@EnvironmentObject var messageState: MessageState
@EnvironmentObject var fetchedMessagesState: FetchedMessagesState
Expand Down
1 change: 0 additions & 1 deletion app/newHere1/newHere/Friends.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import SwiftUI

struct Friends: View {
@Binding var isPresented: Bool
@Binding var userId: String

// State to hold the friends list
@State private var friendsList: [String] = []
Expand Down
5 changes: 2 additions & 3 deletions app/newHere1/newHere/Home.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@ struct HomePageView: View {
@State private var isShowingProfile = false
@State private var isShowingMessages = false
@State private var isShowingPosts = false
@State var userId = ""
@StateObject var messageState = MessageState()
@StateObject var fetchedMessagesState = FetchedMessagesState()

@EnvironmentObject var locationDataManager: LocationDataManager

/// The body of the view, presenting the AR view along with overlay controls for navigation and interaction.
var body: some View {
CustomARViewRepresentable(userId: $userId)
CustomARViewRepresentable()
.environmentObject(messageState)
.environmentObject(fetchedMessagesState)
.overlay(alignment: .bottom){
Expand Down Expand Up @@ -86,7 +85,7 @@ struct HomePageView: View {
}
// Render popups upon state variables being true.
.sheet(isPresented: $isShowingProfile) {
ProfilePopup(isPresented: $isShowingProfile, userId: $userId) // Pass the binding to control visibility
ProfilePopup(isPresented: $isShowingProfile) // Pass the binding to control visibility
}
.sheet(isPresented: $isShowingMessages) {
MessagesPopup(isPresented: $isShowingMessages)
Expand Down
45 changes: 20 additions & 25 deletions app/newHere1/newHere/LoginView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import SwiftUI
let loginUrlString = "https://here-swe.vercel.app/auth/login"
let logApiKey = "qe5YT6jOgiA422_UcdbmVxxG1Z6G48aHV7fSV4TbAPs"

let userId = UserDefaults.standard.string(forKey: "UserId") ?? ""
let userName = UserDefaults.standard.string(forKey: "UserName") ?? ""

struct LoginView: View {
// User input fields
@State internal var username: String = ""
Expand All @@ -23,7 +26,6 @@ struct LoginView: View {
// State variables
@State internal var isRegistered = false
@Binding var isAuthenticated: Bool
@Binding var user_id: String

// Alert properties to display login status
@State internal var showingAlert = false
Expand Down Expand Up @@ -64,7 +66,7 @@ struct LoginView: View {
.padding(.horizontal)

// Navigation link to registration view
NavigationLink(destination: RegistrationView(isRegistered: $isRegistered, userId: $user_id)) {
NavigationLink(destination: RegistrationView(isRegistered: $isRegistered)) {
Text("Don't have an account? Signup")
}
.padding()
Expand Down Expand Up @@ -122,36 +124,29 @@ struct LoginView: View {
let statusCode = httpResponse.statusCode
if statusCode == 200 {
if let data = data {
if let responseString = String(data: data, encoding: .utf8) {
print("Login Response: \(responseString)")

if let jsonData = responseString.data(using: .utf8) {
do {
if let json = try JSONSerialization.jsonObject(with: jsonData, options:[]) as? [String: Any],
let extractedUserId = json["_id"] as? String {
do {
// Attempt to parse the response data as a JSON object.
if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] {
// Try to extract the user ID from the JSON object.
// The key "_id" should match the key provided in the JSON response.
if let extractedUserId = json["_id"] as? String {
print("User id: \(extractedUserId)")
// Extract and store user ID
self.user_id = extractedUserId;
print("updated: \(self.user_id)")
UserDefaults.standard.set(extractedUserId, forKey: "UserId")
// self.isAuthenticated = true;
}
// Store the extracted user ID in UserDefaults.
UserDefaults.standard.set(extractedUserId, forKey: "UserId")
}

if let json = try JSONSerialization.jsonObject(with: jsonData, options:[]) as? [String: Any],
let userName = json["userName"] as? String {
// Similarly, try to extract the user's username from the JSON object.
if let userName = json["userName"] as? String {
print("User Name:\(userName)")
// Store user's username
// Store the extracted username in UserDefaults.
UserDefaults.standard.set(userName, forKey: "UserName")
}
} catch {
print("Error parsing JSON: \(error)")
}
} catch {
// If JSON parsing fails, print an error message.
print("Error parsing JSON: \(error)")
}
}
// Set the user as authenticated
self.isAuthenticated = true;
print("authenticated: \(self.isAuthenticated)")
print("user_id: \(self.user_id)")
self.isAuthenticated = true
}
}else if statusCode == 404 {
// Handle user not found
Expand Down
6 changes: 1 addition & 5 deletions app/newHere1/newHere/Profile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@

import SwiftUI

let userId = UserDefaults.standard.string(forKey: "UserId") ?? ""
let userName = UserDefaults.standard.string(forKey: "UserName") ?? ""

struct ProfilePopup: View {
@Binding var isPresented: Bool // Added binding to control visibility
@State private var isShowingFriends: Bool = false
@Binding var userId: String

var body: some View {
ZStack {
Expand Down Expand Up @@ -55,7 +51,7 @@ struct ProfilePopup: View {
}
.sheet(isPresented: $isShowingFriends) {
// Friends.swift => Friends struct
Friends(isPresented: $isShowingFriends, userId: $userId) // Pass the binding to control visibility
Friends(isPresented: $isShowingFriends) // Pass the binding to control visibility
}
}
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .top) // Adjust size and alignment
Expand Down
3 changes: 1 addition & 2 deletions app/newHere1/newHere/Registration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ struct RegistrationView: View {
@State private var password: String = ""
@State private var confirmPassword: String = ""
@Binding var isRegistered: Bool
@Binding var userId: String
@State private var isAuthenticated = false

@State private var showingAlert = false
Expand Down Expand Up @@ -59,7 +58,7 @@ struct RegistrationView: View {
Text("Submit")
}

NavigationLink(destination: LoginView(isAuthenticated: $isAuthenticated, user_id: $userId)) {
NavigationLink(destination: LoginView(isAuthenticated: $isAuthenticated)) {
Text("Already have an account? Login")
}
}
Expand Down

0 comments on commit beb4350

Please sign in to comment.