basic auth working with google sign in button

This commit is contained in:
talksik
2021-12-13 14:27:39 -08:00
parent fd5699a6f0
commit e566de6b92
10 changed files with 211 additions and 28 deletions
+95 -20
View File
@@ -6,38 +6,101 @@
//
import SwiftUI
import Firebase
import AuthenticationServices
import GoogleSignIn
struct SignInView: View {
var body: some View {
OnboardingTemplateView(hdrText: "Let's get started", imgName: "undraw_enter_uhqk", bottomActArea:
AnyView(
VStack(alignment: .center) {
NavigationLink(destination: HomeView()) {
Text("Sign Up")
.bold()
.foregroundColor(NirvanaColor.white)
.frame(maxWidth: .infinity)
.padding(.vertical, 20)
.background(NirvanaColor.teal)
.clipShape(Capsule())
.shadow(radius:10)
}
Button(
action: {
print("link to website learn more clicked")
},
label: {
Text("Learn More")
// SignInWithAppleButton(.signIn,
// onRequest: { request in
// print("siginin in ")
// }, onCompletion: {result in
// print("signed in")
// })
// .frame(height:50)
// .clipShape(Capsule())
// .shadow(radius:20)
Button {
self.handleLogin()
} label: {
HStack {
Image("google")
.resizable()
.renderingMode(.template)
.aspectRatio(contentMode: .fit)
.frame(width: 28, height: 28)
.foregroundColor(NirvanaColor.white)
Text("Continue with Google")
.foregroundColor(NirvanaColor.white)
.bold()
})
//learn more button to usenirvana.com
}
.frame(maxWidth: .infinity)
.padding(.vertical, 20)
.background(NirvanaColor.teal)
.clipShape(Capsule())
.shadow(radius:10)
}
Text("By continuing, you are agreeing to our Terms of Service.")
.font(.callout.bold())
.foregroundColor(.gray)
.multilineTextAlignment(.center)
}
.padding(.top, 20)
)
)
}
func handleLogin(){
//Google sign in
guard let clientID = FirebaseApp.app()?.options.clientID else { return }
// create google sign in configuration object
let config = GIDConfiguration(clientID: clientID)
GIDSignIn.sharedInstance.signIn(with: config, presenting: getRootViewController())
{[self] user, err in
if let error = err {
print(err)
return
}
guard
let authentication = user?.authentication,
let idToken = authentication.idToken
else {
return
}
let credential = GoogleAuthProvider.credential(withIDToken: idToken,
accessToken: authentication.accessToken)
Auth.auth().signIn(with: credential) { result, error in
if let error = error {
print(error.localizedDescription)
}
guard let user = result?.user else {
return
}
print(user.displayName ?? "Success!")
print(user.photoURL)
// User is signed in
// ...
}
}
}
}
struct SignInView_Previews: PreviewProvider {
@@ -45,3 +108,15 @@ struct SignInView_Previews: PreviewProvider {
SignInView()
}
}
extension View {
func getRootViewController() -> UIViewController {
guard let screen = UIApplication.shared.connectedScenes.first as? UIWindowScene else { return .init() }
guard let root = screen.windows.first?.rootViewController else {
return .init()
}
return root
}
}
@@ -43,6 +43,8 @@ struct OnboardingTemplateView: View {
HeaderView()
VStack {
self.onboardingHeaderText
self.onboardingImageView
self.onboardingActionTextView
@@ -51,7 +53,7 @@ struct OnboardingTemplateView: View {
self.bottomActionArea
}
.padding(screenWidth * 0.1)
.padding(.horizontal, screenWidth * 0.1)
Spacer()
} //outermost vstack
@@ -60,6 +62,19 @@ struct OnboardingTemplateView: View {
.navigationBarTitleDisplayMode(.inline)
}
private var onboardingHeaderText: some View {
ZStack {
if (self.headerText != nil) {
Text(self.headerText!)
.font(.title)
.fontWeight(.medium)
.foregroundColor(NirvanaColor.teal)
.multilineTextAlignment(.center)
}
}
.padding()
}
private var onboardingImageView: some View {
Image(self.imageName ?? "")
.renderingMode(.original)
@@ -98,7 +113,7 @@ struct OnboardingTemplateView: View {
struct OnboardingTemplateView_Previews: PreviewProvider {
static var previews: some View {
OnboardingTemplateView(imgName: "undraw_friendship_mni7", mainLeadingActText: "Your", mainHighlightedActText: "minimalist", mainTrailingActText: "social media.", subActText: "Tired of the rat race on the test of th4e best oadf the best?", bottomActArea: AnyView(
OnboardingTemplateView(hdrText: "let's get started", imgName: "undraw_friendship_mni7", mainLeadingActText: "Your", mainHighlightedActText: "minimalist", mainTrailingActText: "social media.", subActText: "Tired of the rat race on the test of th4e best oadf the best?", bottomActArea: AnyView(
VStack(alignment: .center) {
NavigationLink(destination: HomeView()) {
Text("Start Your Detox")
@@ -14,7 +14,7 @@ struct WelcomeView: View {
var body: some View {
OnboardingTemplateView(imgName: "undraw_friendship_mni7", mainLeadingActText: "Your", mainHighlightedActText: "minimalist", mainTrailingActText: "social media.", subActText: "Tired of the rat race on insta, snap, tik-tok, \"meta\"?", bottomActArea: AnyView(
VStack(alignment: .center) {
NavigationLink(destination: HomeView()) {
NavigationLink(destination: SignInView()) {
Text("Start Your Detox")
.bold()
.foregroundColor(NirvanaColor.white)