fnakstad / angular-client-side-auth

One way to implement authentication/authorization in Angular applications
http://angular-client-side-auth.herokuapp.com/
MIT License
1.63k stars 346 forks source link

Can you explain what the regex is doing in the User.js validate function? #80

Closed cleversprocket closed 10 years ago

cleversprocket commented 10 years ago

I'm terrible at understanding long regex and I was unable to enter a correct username with WebStorm's check regex feature. It's located in server/models/User.js:

validate: function(user) { ... check(user.username, 'Invalid username').not(/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/); }

Thanks in advance!

fnakstad commented 10 years ago

This was to prevent URL's to be registered as usernames. In the example app which is hosted on Heroku, I display a list of usernames in the user list view. I didn't want spammers entering URL's as usernames, and then having them be visible on this page: http://angular-client-side-auth.herokuapp.com/admin/

In terms of actual applications derived from this project, there's no problem changing it to something less strict :)

cleversprocket commented 10 years ago

Does it matter that example.com is valid with this regex? Thanks

fnakstad commented 10 years ago

I think it's okay if it's only domain names :) This measure was just to prevent/reduce the possibility of anyone linkbaiting on the demo site. In addition the users saved in memory on the server are reset at a set interval.