redstone-dart / redstone

A metadata driven microframework for Dart.
http://redstone-dart.github.io/redstone
MIT License
342 stars 42 forks source link

granting access to mirrors within routes #195

Closed jodinathan closed 7 years ago

jodinathan commented 7 years ago

I needed access to the list of routes and their mirrors so I create a private and public route system.

Example:

in main:


// search for public routes
    app.router.targets.forEach((target) {
        if (target.methodMirror.metadata.any((m) => m.reflectee is Public)) {
            print('adding ${target.contextUrl}, ${target.template}');
            publicRoutes.add(target.template);
        }
    });

some interceptor:

@app.Interceptor(r'/.*')
interceptor() async {
    bool hasUser = app.request.session["user"] != null;
    bool isPublic = publicRoutes.any(
            (UrlTemplate templ) => templ.match('/' + app.request.url.path) != null
    );

    if (!hasUser && !isPublic)
        return app.chain.abort(HttpStatus.UNAUTHORIZED);
}
Pacane commented 7 years ago

Interesting! Thanks :)