vapor-community / Imperial

Federated Authentication with OAuth providers
MIT License
155 stars 48 forks source link

How can I get Google authURL? #38

Closed gaetandezeiraud closed 3 years ago

gaetandezeiraud commented 5 years ago

Hi, I have setup like indicated in the Google guide. When I'm adding a route in an protected route (with ImperialMiddleware) , I get following message User currently not authenticated. But, I want to redirect the user to the Google Login Page, for authentification.

But, I can not access to authURL in GoogleRouter for set this value in ImperialMiddleware(redirect: <#T##String?#>). How can I do it?

Thanks.

calebkleveter commented 5 years ago

The redirect parameter in the ImperialMiddleware initializer is the path to redirect the client to if they are not authenticated.

I have outlined how to actually authenticate the user in the Google auth guide.

Basically you register the auth route like this:

try router.oAuth(from: Google.self, authenticate: "google", callback: "http://localhost:8080/google-complete")

Then the client can go to the /google route, which will redirect them to Google for sign-in.

gaetandezeiraud commented 5 years ago

Ok I see, thanks for your awnser. But, I need to create the /google route? Because by default, I have just the message in log and not redirection. My code is:

try router.oAuth(from: Google.self, authenticate: "google", callback: "http://localhost:8080/google-complete", scope: ["https://www.googleapis.com/auth/cloud-platform"]) { (request, token) in
 print(token)
 return request.future(request.redirect(to: "/"))
 }

And

let protected = router.grouped(ImperialMiddleware())
protected.get("hello") { req in
        return "Hello, world!"
}

And I get {"error":true,"reason":"User currently not authenticated"} but no redirection.

If I need to create this route, how I can get the authURL available in GoogleRouter.swift with clientID, scope, etc?

Thanks you, for the project and your quick response!

calebkleveter commented 5 years ago

The /google route is automatically created by the router.oAuth call. You should be good to go!

gaetandezeiraud commented 5 years ago

OK, thanks for the details! I'll be back soon with a merger request for Keycloak support.