Code Monkey home page Code Monkey logo

auth-example's People

Contributors

sfaxon avatar tanner0101 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

auth-example's Issues

Vapor 2 Support

I tried to follow this example. My project built with vapor 2, but this example is built with Vapor 1.x(as most of the vapor-community examples).

Could anyone update this example to use vapor 2.x?

AuthError.noSubject

I am trying to create a login route. I have successfully created the register portion, but am struggling to determine what has gone wrong on the login side of things.

I have the protect and auth middleware setup on the drop, as well as in the middleware.json file. Here is the code for my login route:

users.post("login") { req in
        guard let username = req.data["username"]?.string else {
            throw Abort.badRequest
        }

        guard let password = req.data["password"]?.string else {
            throw Abort.badRequest
        }

        let creds = UsernamePassword(username: username, password: password)

        try req.auth.login(creds)

        let u = try req.user()

        return try JSON(node: ["message": "\(u.name) Logged in."])
    }

Which is all wrapped within a group called users.

Here's the weird thing. It always works the first time after clearing the cache, but then fails the second time (after succeeding the first time) and results in an error:

Uncaught Error: AuthError.noSubject. Use middleware to catch this error and provide a better response. Otherwise, a 500 error page will be returned in the production environment.

What am I doing wrong?? Here is my authenticate function on my User model:

    static func authenticate(credentials: Credentials) throws -> Auth.User {
        let user: User?

        switch credentials {
        case let usernamePassword as UsernamePassword:
            print(usernamePassword.password + usernamePassword.username)
            user = try User.query().filter("email", usernamePassword.username).filter("password", usernamePassword.password).first()
        default:
            throw Abort.custom(status: .badRequest, message: "Invalid credentials.")
        }

        guard let u = user else {
            throw Abort.custom(status: .badRequest, message: "User not found")
        }

        return u
    }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.