Skip to content

Commit

Permalink
Merge pull request #152 from dbrgn/news-opt-in
Browse files Browse the repository at this point in the history
Allow opting in to news
  • Loading branch information
dbrgn authored Mar 17, 2024
2 parents 87d41e3 + b116a8f commit 5c83e10
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 8 deletions.
2 changes: 1 addition & 1 deletion diesel.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[print_schema]
file = "src/schema.rs"
patch_file = "src/schema.patch"
#patch_file = "src/schema.patch"
import_types = ["diesel::sql_types::*", "diesel_geography::sql_types::*"]
29 changes: 24 additions & 5 deletions frontend/src/routes/auth/registration/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
let email: string = '';
let password1: string = '';
let password2: string = '';
let newsletter: boolean = false;
// Element bindings
let flashes: Flashes;
Expand Down Expand Up @@ -97,7 +98,7 @@
console.log('Registering via API');
let registrationResult;
try {
registrationResult = await apiRegister(username, email, password1);
registrationResult = await apiRegister(username, email, password1, newsletter);
} catch (error) {
submitError = {
type: 'api-error',
Expand Down Expand Up @@ -185,7 +186,7 @@
{/if}

<div class="field">
<label class="label" for="username">E-mail</label>
<label class="label" for="email">E-mail</label>
<div class="control has-icons-left">
<input
id="email"
Expand All @@ -205,7 +206,7 @@
{/if}

<div class="field">
<label class="label" for="password">Password</label>
<label class="label" for="password1">Password</label>
<div class="control has-icons-left">
<input
id="password1"
Expand All @@ -226,7 +227,7 @@
{/if}

<div class="field">
<label class="label" for="password">Password Confirmation</label>
<label class="label" for="password2">Password Confirmation</label>
<div class="control has-icons-left">
<input
id="password2"
Expand All @@ -246,7 +247,16 @@
<div class="field-error">Error: {fieldErrors.password2}</div>
{/if}

<p class="content">
<div class="field newsletter">
<div class="control has-icons-left">
<label class="checkbox" for="newsletter">
<input id="newsletter" type="checkbox" bind:checked={newsletter} />
I want to receive occasional news about Flugbuech through e-mail
</label>
</div>
</div>

<p class="content privacy-policy-hint">
By registering, you acknowledge the <a href="/privacy-policy/">privacy policy</a>.
</p>
<div class="field">
Expand All @@ -267,4 +277,13 @@
margin-top: -12px;
margin-bottom: 12px;
}
.field.newsletter {
margin-top: 2em;
}
.privacy-policy-hint {
margin-top: 2em;
font-style: italic;
}
</style>
2 changes: 2 additions & 0 deletions frontend/src/routes/auth/registration/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ export async function apiRegister(
username: string,
email: string,
password: string,
newsOptIn: boolean,
): Promise<RegistrationResult> {
const res = await apiPost('/api/v1/auth/registration', {
username,
email,
password,
newsOptIn,
});
switch (res.status) {
case 204:
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/routes/profile/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
<th>Registered Since</th>
<td>{data.profile.signedUp}</td>
</tr>
<tr>
<th>Newsletter</th>
<td>{data.profile.newsOptIn ? 'Yes' : 'No'}</td>
</tr>
</tbody>
</table>
</div>
1 change: 1 addition & 0 deletions frontend/src/routes/profile/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const SCHEMA_API_PROFILE = z.object({
.string()
.transform((datestring) => new Date(datestring))
.optional(),
newsOptIn: z.boolean(),
});

export type Profile = z.infer<typeof SCHEMA_API_PROFILE>;
Expand Down
2 changes: 2 additions & 0 deletions migrations/2024-03-17-222039_allow-news-checkbox/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE users
DROP COLUMN news_opt_in;
2 changes: 2 additions & 0 deletions migrations/2024-03-17-222039_allow-news-checkbox/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE users
ADD COLUMN news_opt_in boolean NOT NULL DEFAULT false;
2 changes: 2 additions & 0 deletions src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub struct Registration {
username: String,
email: String,
password: String,
news_opt_in: bool,
}

#[derive(Serialize, Deserialize, Clone)]
Expand Down Expand Up @@ -204,6 +205,7 @@ pub async fn registration(
&registration.username,
&registration.email,
&registration.password,
registration.news_opt_in,
)
})
.await;
Expand Down
2 changes: 2 additions & 0 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,14 @@ pub fn create_user(
username: impl Into<String>,
email: impl Into<String>,
password: impl Into<String>,
news_opt_in: bool,
) -> User {
diesel::insert_into(users::table)
.values(&(
users::username.eq(username.into()),
users::password.eq(crypt(password.into(), gen_salt("bf", PW_SALT_ITERATIONS))),
users::email.eq(email.into()),
users::news_opt_in.eq(news_opt_in),
))
.get_result(conn)
.expect("Could not create user")
Expand Down
2 changes: 2 additions & 0 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub struct User {
pub email: String,
/// When the user was created
pub signed_up: DateTime<Utc>,
/// Whether the user has opted in to receive news
pub news_opt_in: bool,
}

#[derive(Identifiable, Queryable, Associations, AsChangeset, Serialize, PartialEq, Debug, Clone)]
Expand Down
2 changes: 2 additions & 0 deletions src/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct ApiProfile {
username: String,
email: String,
signed_up: DateTime<Utc>,
news_opt_in: bool,
}

#[get("/profile")]
Expand All @@ -21,6 +22,7 @@ pub fn get(user: auth::AuthUser) -> Json<ApiProfile> {
username: user.username,
email: user.email,
signed_up: user.signed_up,
news_opt_in: user.news_opt_in,
})
}

Expand Down
1 change: 1 addition & 0 deletions src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ table! {
last_glider_id -> Nullable<Int4>,
email -> Text,
signed_up -> Timestamptz,
news_opt_in -> Bool,
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ impl<'a> DbTestContext<'a> {
.expect("Could not run database migrations");

// Create test user
let testuser1 = create_user(&mut conn, "testuser1", "user1@example.com", "testpass");
let testuser2 = create_user(&mut conn, "testuser2", "user2@example.com", "testpass");
let testuser1 = create_user(&mut conn, "testuser1", "user1@example.com", "testpass", false);
let testuser2 = create_user(&mut conn, "testuser2", "user2@example.com", "testpass", false);

DbTestContext {
conn: Mutex::new(conn),
Expand Down

0 comments on commit 5c83e10

Please sign in to comment.