| 1 | // Copyright (c) 2019-2026 Alexander Medvednikov. All rights reserved. |
| 2 | // Use of this source code is governed by a GPL license that can be found in the LICENSE file. |
| 3 | module main |
| 4 | |
| 5 | import veb |
| 6 | |
| 7 | @['/:username/settings/api-tokens'] |
| 8 | pub fn (mut app App) view_api_tokens(mut ctx Context, username string) veb.Result { |
| 9 | if !ctx.logged_in || ctx.user.username != username { |
| 10 | return ctx.redirect_to_index() |
| 11 | } |
| 12 | tokens := app.list_user_api_tokens(ctx.user.id) |
| 13 | new_token := ctx.query['new_token'] or { '' } |
| 14 | return $veb.html('templates/api_tokens.html') |
| 15 | } |
| 16 | |
| 17 | @['/:username/settings/api-tokens'; post] |
| 18 | pub fn (mut app App) handle_create_api_token(mut ctx Context, username string) veb.Result { |
| 19 | if !ctx.logged_in || ctx.user.username != username { |
| 20 | return ctx.redirect_to_index() |
| 21 | } |
| 22 | name := ctx.form['name'].trim_space() |
| 23 | if name == '' { |
| 24 | return ctx.redirect('/${username}/settings/api-tokens') |
| 25 | } |
| 26 | _, plain := app.add_api_token(ctx.user.id, name) or { |
| 27 | return ctx.redirect('/${username}/settings/api-tokens') |
| 28 | } |
| 29 | return ctx.redirect('/${username}/settings/api-tokens?new_token=${plain}') |
| 30 | } |
| 31 | |
| 32 | @['/:username/settings/api-tokens/:id/delete'; post] |
| 33 | pub fn (mut app App) handle_delete_api_token(mut ctx Context, username string, id string) veb.Result { |
| 34 | if !ctx.logged_in || ctx.user.username != username { |
| 35 | return ctx.redirect_to_index() |
| 36 | } |
| 37 | app.delete_api_token(ctx.user.id, id.int()) or {} |
| 38 | return ctx.redirect('/${username}/settings/api-tokens') |
| 39 | } |
| 40 | |