vapor-community / Imperial

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

Completion Closure Not Invoked When Using Google Auth #84

Closed rwbutler closed 3 years ago

rwbutler commented 3 years ago

At the moment, I'm trying to use Imperial to allow users to sign in to me web app using Google Auth. I've set up my code as follows (see below). The issue I'm experiencing at the moment is that processGoogleLogin is never fired. If I navigate to /google then I do see the OAuth page and on logging in using my Google account I am redirected to /authenticated which is correct but I never received an access token back it would seem and completion closure never appears to fire. Is there a piece of the set up that I am missing perhaps?

Thanks in advance 🙏

routes.swift

let imperialController = ImperialController(sessionsMiddleware: app.sessions.middleware)
try appRoutes.register(collection: imperialController)

ImperialController.swift

class ImperialController {
    private let sessionsMiddleware: Middleware

    init(sessionsMiddleware: Middleware) {
        self.sessionsMiddleware = sessionsMiddleware
    }

    func processGoogleLogin(request: Request, token: String) throws -> EventLoopFuture<ResponseEncodable> {
        print(token)
        return request.eventLoop.future(request.redirect(to: "/"))
    }
}

extension ImperialController: RouteCollection {
    func boot(routes: RoutesBuilder) throws {
        let group = routes.grouped(sessionsMiddleware)
        try group.oAuth(
            from: Google.self,
            authenticate: "google",
            authenticateCallback: nil,
            callback: "http://localhost:8080/authenticated",
            scope: ["profile", "email"],
            completion: processGoogleLogin
        )
    }
}
rwbutler commented 3 years ago

I've realised that this issue is a manifestation of https://github.com/vapor-community/Imperial/issues/43 - so long as route groups aren't used then this will work.