Help on my login code (I am only starting out)

Hi there,

I am making an app on xcodejust to start that has a login that lets you into the "main page" and then you can do things.

This is my code so far:

import SwiftUI

struct ContentView: View {
@State private var username = ""
@State private var password = ""
@State private var wrongUsername: Float = 0
@State private var wrongPassword: Float = 0
@State private var showingLoginScreen = false

var body: some View {
    NavigationView {
        ZStack {
            Color.blue
                .ignoresSafeArea()
            Circle()
                .scale(1.7)
                .foregroundColor(.white.opacity(0.15))
            Circle()
                .scale(1.35)
                .foregroundColor(.white)

            VStack {
                Text("Login")
                    .font(.largeTitle)
                    .bold()
                    .padding()
                
                TextField("Username", text: $username)
                    .padding()
                    .frame(width: 300, height: 50)
                    .background(Color.black.opacity(0.05))
                    .cornerRadius(10)
                    .border(.red, width: CGFloat(wrongUsername))
                    
                
                SecureField("Password", text: $password)
                    .padding()
                    .frame(width: 300, height: 50)
                    .background(Color.black.opacity(0.05))
                    .cornerRadius(10)
                    .border(.red, width: CGFloat(wrongPassword))
                
                Button("Login") {
                    authenticateUser(username: username, password: password)
                    }
                .foregroundColor(.white)
                .frame(width: 300, height: 50)
                .background(Color.blue)
                .cornerRadius(10)
                
                NavigationLink(destination: Text("You are logged in @\(username)"), isActive: $showingLoginScreen) {
                    EmptyView()
                }
            }
        }.navigationBarHidden(true)
    }
}

func authenticateUser(username: String, password: String) {
    if username.lowercased() == "admin" {
        wrongUsername = 0
        if password.lowercased() == "admin" {
            wrongPassword = 0
            showingLoginScreen = true
        } else {
            wrongPassword = 2
        }
    } else {
        wrongUsername = 2
    }
}

}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

How would I go around when its logged in to navigate to another GUI it atm is just displaying text as a test.

Any help would be great!!!
Oli

Ps: no idea what category to pick sorry :)

1 Like

Hi,

I'm not sure if this is what you're asking for but you could do something like

struct AnotherView: View {
    var body: some View {
        Text("You're logged in!")
    }
}

struct ContentView: View {

    ...

    @State private var isLoggedIn = false

    var body: some View {
        ...

        NavigationLink(destination: AnotherView(), isActive: $isLoggedIn) {
            EmptyView()
        }

        ...
    }
}
1 Like

Thank you so much for coming the help!!! Where would this slot in in the body of code??

Thanks a million!!

You can place it anywhere you want in the body :slight_smile:

I am now getting another error "invalid redeclaration of content view.

Code:

import SwiftUI

struct ContentView: View {
@State private var username = ""
@State private var password = ""
@State private var wrongUsername: Float = 0
@State private var wrongPassword: Float = 0
@State private var showingLoginScreen = false

var body: some View {
    NavigationView {
        ZStack {
            Color.blue
                .ignoresSafeArea()
            Circle()
                .scale(1.7)
                .foregroundColor(.white.opacity(0.15))
            Circle()
                .scale(1.35)
                .foregroundColor(.white)

            VStack {
                Text("Login")
                    .font(.largeTitle)
                    .bold()
                    .padding()
                
                TextField("Username", text: $username)
                    .padding()
                    .frame(width: 300, height: 50)
                    .background(Color.black.opacity(0.05))
                    .cornerRadius(10)
                    .border(.red, width: CGFloat(wrongUsername))
                    
                
                SecureField("Password", text: $password)
                    .padding()
                    .frame(width: 300, height: 50)
                    .background(Color.black.opacity(0.05))
                    .cornerRadius(10)
                    .border(.red, width: CGFloat(wrongPassword))
                
                Button("Login") {
                    authenticateUser(username: username, password: password)
                    }
                .foregroundColor(.white)
                .frame(width: 300, height: 50)
                .background(Color.blue)
                .cornerRadius(10)
                
                NavigationLink(destination: Text("You are logged in @\(username)"), isActive: $showingLoginScreen) {
                    EmptyView()
                }
            }
        }.navigationBarHidden(true)
    }
}

func authenticateUser(username: String, password: String) {
    if username.lowercased() == "admin" {
        wrongUsername = 0
        if password.lowercased() == "admin" {
            wrongPassword = 0
            showingLoginScreen = true
        } else {
            wrongPassword = 2
        }
    } else {
        wrongUsername = 2
    }
}

}
struct AnotherView: View {
var body: some View {
Text("You're logged in!")
}
}

struct ContentView: View {

@State private var isLoggedIn = false

var body: some View {
    

    NavigationLink(destination: AnotherView(), isActive: $isLoggedIn) {
        EmptyView()
    }

    
}

}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

invalid redeclaration of content view - you cannot have two structs named ContentView