Allyedge

Allyedge

Is there a way to add new fields to phx.gen.auth easier?

I added an username field and it works just fine, but it took quite a time and I had to update a lot of stuff for a simple username.

Also, there are like 110 tests that don’t include the new fields and I really don’t want to change them manually.

Is there an easier way to change them? Or should I just remove all of them and only add the ones that are really important?

Thanks!

Showing Posts 1 to 10

josevalim

josevalim

Creator of Elixir

Do you mean 110 tests started failing or rather that 110 tests needed changing? If so, the question would be why the 110 tests would need to change. :slight_smile:

Allyedge

Allyedge OP

I mean, since the tests are only for the email and password fields, I think I have to update most of them, if not all of them.

And yeah, most of them are failing because of the changes I’ve made :slightly_smiling_face:

dimitarvp

dimitarvp

Can’t advise you on how to change 110 existing tests but it’s a good opportunity to make test helper functions that assert on certain common structs and conditions and start replacing all hardcoded tests with using those helper functions. That way in the Future™ you would only change things in 2-3 places.

Allyedge

Allyedge OP

Alright, I see.

Thanks!

sodapopcan

sodapopcan

I did this recently and feel like I only updated a few tests (maybe five or six?). So long as email remains a key identifier of a Accounts.User, you shouldn’t need to go adding username everywhere you see email and password (or even just email) in the tests. So long as you have some tests asserting that a username is required, present, and unique, that should be enough coverage around it. Apart from that, you just need to update the fixture to ensure you don’t have to worry about adding a username for tests that aren’t concerned with it.

Of course, if you are going to allow users to login with their username instead of their password, that’s a different story. It still shouldn’t be that many test changes. You’d need to update any place that fetches by username/password, but once you have a loaded user, the rest of the functions that use email can stay the same.

Another route I’ve taken in a multi-tenant app is to leave the Accounts context completely alone. Once a user logs in with their email and password, they become an OrgMember in an Orgs context (for example) and choose a username post-registration. OrgMember can either be its own table or it could point to the users table (there are pros and cons to both ways). In either scenario, the only thing OrgMember schema needs to know from User is email. This way, way you have really clean separation between authorization and the rest of your app and you don’t have to futz about with generated code.

Allyedge

Allyedge OP

Thanks for the answer! I guess I shouldn’t add username to everything that has email and password in it.

Allyedge

Allyedge OP

Update, I updated the fixture and some other tests and now I only have around 15 tests that fail :slightly_smiling_face:

Thanks for the help!

BLUECALF

BLUECALF

Hello a asking , the way in

mix phx.gen.html Accounts Driver drivers name:string age:int status:string

Makes schema,html,and migration for Drivers

why can’t we do that again with auth.

What if we need to seriously authenticate those Drivers.

Do you know an easier way. I don want o make login and signups from scratch.

al2o3cr

al2o3cr

Generating working code from nothing is easy, generating working code on top of existing code (like the Driver schema created by phx.gen.html) is massively harder.

You could try running phx.gen.auth in a brand-new Phoenix project and copy over the relevant bits of the output to your current project.

BLUECALF

BLUECALF

Thanks
I did phx.gen.auth Accounts User users

Then changed users schema, to have other fields like first name ,last…, Gender

Then changed the html.heex to have those inputs

Then changed some generated code through controller to take in those
fields like [:first name, :gender]

it worked well as functioning register and login .

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews