Chapters
33 Chapters
|
3:34:13
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!Compatible PHP versions: ^7.1.3
Subscribe to download the code!Compatible PHP versions: ^7.1.3
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
25.
ApiToken Entity
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": "^7.1.3",
"ext-iconv": "*",
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"doctrine/annotations": "^1.0", // 1.10.2
"doctrine/doctrine-bundle": "^1.6.10", // 1.10.2
"doctrine/doctrine-migrations-bundle": "^1.3|^2.0", // v2.0.0
"doctrine/orm": "^2.5.11", // v2.7.2
"knplabs/knp-markdown-bundle": "^1.7", // 1.7.0
"knplabs/knp-paginator-bundle": "^2.7", // v2.8.0
"knplabs/knp-time-bundle": "^1.8", // 1.8.0
"nexylan/slack-bundle": "^2.0,<2.2.0", // v2.0.0
"php-http/guzzle6-adapter": "^1.1", // v1.1.1
"phpdocumentor/reflection-docblock": "^3.0|^4.0", // 4.3.0
"sensio/framework-extra-bundle": "^5.1", // v5.2.0
"stof/doctrine-extensions-bundle": "^1.3", // v1.3.0
"symfony/asset": "^4.0", // v4.1.4
"symfony/cache": "^3.3|^4.0", // v4.1.4
"symfony/console": "^4.0", // v4.1.4
"symfony/flex": "^1.0", // v1.21.6
"symfony/framework-bundle": "^4.0", // v4.1.4
"symfony/lts": "^4@dev", // dev-master
"symfony/property-access": "^3.3|^4.0", // v4.1.4
"symfony/property-info": "^3.3|^4.0", // v4.1.4
"symfony/security-bundle": "^4.0", // v4.1.4
"symfony/serializer": "^3.3|^4.0", // v4.1.4
"symfony/twig-bundle": "^4.0", // v4.1.4
"symfony/web-server-bundle": "^4.0", // v4.1.4
"symfony/yaml": "^4.0", // v4.1.4
"twig/extensions": "^1.5" // v1.5.2
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.0", // 3.0.2
"easycorp/easy-log-handler": "^1.0.2", // v1.0.7
"fzaninotto/faker": "^1.7", // v1.8.0
"symfony/debug-bundle": "^3.3|^4.0", // v4.1.4
"symfony/dotenv": "^4.0", // v4.1.4
"symfony/maker-bundle": "^1.0", // v1.7.0
"symfony/monolog-bundle": "^3.0", // v3.3.0
"symfony/phpunit-bridge": "^3.3|^4.0", // v4.1.4
"symfony/stopwatch": "^3.3|^4.0", // v4.1.4
"symfony/var-dumper": "^3.3|^4.0", // v4.1.4
"symfony/web-profiler-bundle": "^3.3|^4.0" // v4.1.4
}
}
10 Comments
Hi Ryan,
Thank you so much for your fantastic Tutorials!
I have two questions here about "Api Token".
1- Is there any specific reason why the relation between User and ApiToken Entities are "oneToMany" or Could it be also "oneToOne"?
2- By Following your other Tutorials, I have made a Database and Api Platform and also I have a React Native Mobile App with a login screen and an Api back-end! but, I don't know the flow of Authenticate the User from the app to the Api and in combination with retrieving a Token for Authorization! Can you please explain to me in details which steps to take?
Hey Roozbeh S.!
I'm so glad you've found them useful!!!
Yep, could totally be OneToOne. It's just a matter of whether you want to allow a single account to have just one API token or the ability to create many.
First, I have to say that making a mobile app is something that I have not done. I do my best to read up on the strategies, but I'm not fully qualified to answer this question. That being said, for React Native, they have a nice example of authentication - https://reactnavigation.org/docs/auth-flow/ - the key method that you're looking for you can find if you search for
signIn: async data => {<br />. There, they describe it: it can be as simple as sending a POST request with the email & password to an endpoint that validates that and creates (and returns) a token. We create an endpoint like that in this tutorial - https://symfonycasts.com/screencast/symfony-rest4/create-json-web-token. In that tutorial, we are using JWT (instead of tokens stored in the database), but that is NOT important. The important thing is that you have an endpoint that creates a token and returns it. GitHub's API also has this type of functionality where you can send HTTP basic authentication to an endpoint and get back a token - https://lornajane.net/posts/2012/github-api-access-tokens-via-curlLet me know if that helps!
Cheers!
Thank you so much Ryan,
you have explained it very well.
I am going to follow the links you have sent and hopefully I can solve my issues!
I’m very grateful for your respond.
Thank you
Very well explained and it is interesting, thanks!
A question about the "make" command.
Can you generate entity that has configuration in YAML, instead of annotations?
If not, is there an easy way to convert your annotations to YAML configs?
Hey Serge,
No, MakerBundle generates entities in annotation format only, and actually that's best practice to use annotations for your entities. But you can generate an annotated entity with MakerBundle and use "bin/console doctrine:mapping:convert" command to convert it into yaml/xml format.
Cheers!
Some questions about ApiToken entity:
1) Since all fields get their values at the constructor, shouldn't we remove the
questionnullable mark from the getters return type?2) What are your thoughts about making $token the primary key? Or at least a unique key?
Hey IvanPuntiy!
These are great questions!
1) Yes! Definitely - I wasn't even thinking about this / being lazy. We can now remove those - which is awesome!
2) Primary key... maybe... I just personally always like having a specific, meaningless primary key (whether it's an auto-incrementing integer or a uuid). I don't see a lot of benefit into making it primary. But making it unique, heck yea! You won't run into collisions (the odds of that happening are astronomical) but it *will* make querying on this field a bit faster... so why not. As a warning, if you get an error about the unique key length being too long, it's an Innodb + utf8 problem - just make the length of your token field a bit smaller (even 180 is small enough).
Cheers!
I am trying to work through this because I have a project that I need this for and I am just wondering if I am thinking about this correctly?
When I create a new user, I want to automatically create a token. So, I am using teh following Doctrine EventSubscriber and am wondering if there is a better way?
And, for completeness, I added this to services.yaml:
Hey Scott
I don't think there is something wrong with your approach (that's how we encrypt the password for users), although I'm not such a fan of Doctrine listeners, I would probably set that ApiToken right where users are created
Cheers!
"Houston: no signs of life"
Start the conversation!