123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716 |
- <template>
- <div>
- <metadata title="Settings" />
- <main-header />
- <div class="container">
- <div class="nav-links">
- <router-link
- :class="{ active: activeTab === 'profile' }"
- to="#profile"
- >
- Profile
- </router-link>
- <router-link
- :class="{ active: activeTab === 'account' }"
- to="#account"
- >
- Account
- </router-link>
- <router-link
- :class="{ active: activeTab === 'security' }"
- to="#security"
- >
- Security
- </router-link>
- <router-link
- :class="{ active: activeTab === 'preferences' }"
- to="#preferences"
- >
- Preferences
- </router-link>
- </div>
- <div class="content profile-tab" v-if="activeTab === 'profile'">
- <p class="control is-expanded">
- <label for="name">Name</label>
- <input
- class="input"
- id="name"
- type="text"
- placeholder="Name"
- v-model="user.name"
- />
- </p>
- <p class="control is-expanded">
- <label for="location">Location</label>
- <input
- class="input"
- id="location"
- type="text"
- placeholder="Location"
- v-model="user.location"
- />
- </p>
- <p class="control is-expanded">
- <label for="bio">Bio</label>
- <textarea
- class="textarea"
- id="bio"
- placeholder="Bio"
- v-model="user.bio"
- />
- </p>
- <div class="control is-expanded avatar-select">
- <label>Avatar</label>
- <div class="select">
- <select v-if="user.avatar" v-model="user.avatar.type">
- <option value="gravatar">Using Gravatar</option>
- <option value="initials">Based on initials</option>
- </select>
- </div>
- </div>
- <button
- class="button is-primary"
- @click="saveChangesToProfile()"
- >
- Save changes
- </button>
- </div>
- <div class="content account-tab" v-if="activeTab === 'account'">
- <p class="control is-expanded">
- <label for="name">Username</label>
- <input
- class="input"
- id="username"
- type="text"
- placeholder="Username"
- v-model="user.username"
- />
- </p>
- <p class="control is-expanded">
- <label for="location">Email</label>
- <input
- class="input"
- id="email"
- type="text"
- placeholder="Email"
- v-if="user.email"
- v-model="user.email.address"
- />
- </p>
- <button
- class="button is-primary"
- @click="saveChangesToAccount()"
- >
- Save changes
- </button>
- </div>
- <div class="content security-tab" v-if="activeTab === 'security'">
- <label v-if="!password" class="label">Add password</label>
- <div v-if="!password" class="control is-grouped">
- <button
- v-if="passwordStep === 1"
- class="button is-success"
- @click="requestPassword()"
- >
- Request password email
- </button>
- <br />
- <p
- v-if="passwordStep === 2"
- class="control is-expanded has-icon has-icon-right"
- >
- <input
- v-model="passwordCode"
- class="input"
- type="text"
- placeholder="Code"
- />
- </p>
- <p v-if="passwordStep === 2" class="control is-expanded">
- <button
- class="button is-success"
- v-on:click="verifyCode()"
- >
- Verify code
- </button>
- </p>
- <p
- v-if="passwordStep === 3"
- class="control is-expanded has-icon has-icon-right"
- >
- <input
- v-model="setNewPassword"
- class="input"
- type="password"
- placeholder="New password"
- />
- </p>
- <p v-if="passwordStep === 3" class="control is-expanded">
- <button
- class="button is-success"
- @click="setPassword()"
- >
- Set password
- </button>
- </p>
- </div>
- <a
- v-if="passwordStep === 1 && !password"
- href="#"
- @click="passwordStep = 2"
- >Skip this step</a
- >
- <a
- v-if="!github"
- class="button is-github"
- :href="`${serverDomain}/auth/github/link`"
- >
- <div class="icon">
- <img class="invert" src="/assets/social/github.svg" />
- </div>
- Link GitHub to account
- </a>
- <button
- v-if="password && github"
- class="button is-danger"
- @click="unlinkPassword()"
- >
- Remove logging in with password
- </button>
- <button
- v-if="password && github"
- class="button is-danger"
- @click="unlinkGitHub()"
- >
- Remove logging in with GitHub
- </button>
- <br />
- <button
- class="button is-warning"
- style="margin-top: 30px;"
- @click="removeSessions()"
- >
- Log out everywhere
- </button>
- </div>
- <div
- class="content preferences-tab"
- v-if="activeTab === 'preferences'"
- >
- <p class="control is-expanded checkbox-control">
- <input
- type="checkbox"
- id="nightmode"
- v-model="localNightmode"
- />
- <label for="nightmode">
- <span></span>
- <p>Use nightmode</p>
- </label>
- </p>
- <button
- class="button is-primary"
- @click="saveChangesPreferences()"
- >
- Save changes
- </button>
- </div>
- </div>
- <main-footer />
- </div>
- </template>
- <script>
- import { mapState, mapActions } from "vuex";
- import Toast from "toasters";
- import MainHeader from "../MainHeader.vue";
- import MainFooter from "../MainFooter.vue";
- import io from "../../io";
- import validation from "../../validation";
- export default {
- components: { MainHeader, MainFooter },
- data() {
- return {
- user: {},
- originalUser: {},
- newPassword: "",
- password: false,
- github: false,
- setNewPassword: "",
- passwordStep: 1,
- passwordCode: "",
- serverDomain: "",
- activeTab: "",
- localNightmode: false
- };
- },
- computed: mapState({
- userId: state => state.user.auth.userId,
- nightmode: state => state.user.preferences.nightmode
- }),
- mounted() {
- if (this.$route.hash === "") {
- this.$router.push("#profile");
- } else {
- this.activeTab = this.$route.hash.replace("#", "");
- this.localNightmode = this.nightmode;
- lofig.get("serverDomain").then(serverDomain => {
- this.serverDomain = serverDomain;
- });
- io.getSocket(socket => {
- this.socket = socket;
- this.socket.emit("users.findBySession", res => {
- if (res.status === "success") {
- this.user = res.data;
- this.originalUser = JSON.parse(
- JSON.stringify(this.user)
- );
- this.password = this.user.password;
- this.github = this.user.github;
- } else {
- new Toast({
- content: "Your are currently not signed in",
- timeout: 3000
- });
- }
- });
- this.socket.on("event:user.linkPassword", () => {
- this.password = true;
- });
- this.socket.on("event:user.linkGitHub", () => {
- this.github = true;
- });
- this.socket.on("event:user.unlinkPassword", () => {
- this.password = false;
- });
- this.socket.on("event:user.unlinkGitHub", () => {
- this.github = false;
- });
- });
- }
- },
- methods: {
- // switchTab(tabName) {
- // this.activeTab = tabName;
- // },
- saveChangesToProfile() {
- if (this.user.name !== this.originalUser.name) this.changeName();
- if (this.user.location !== this.originalUser.location)
- this.changeLocation();
- if (this.user.bio !== this.originalUser.bio) this.changeBio();
- if (this.user.avatar.type !== this.originalUser.avatar.type)
- this.changeAvatarType();
- },
- saveChangesToAccount() {
- if (this.user.username !== this.originalUser.username)
- this.changeUsername();
- if (this.user.email.address !== this.originalUser.email.address)
- this.changeEmail();
- },
- saveChangesPreferences() {
- if (this.localNightmode !== this.nightmode)
- this.changeNightmodeLocal();
- },
- changeEmail() {
- const email = this.user.email.address;
- if (!validation.isLength(email, 3, 254))
- return new Toast({
- content: "Email must have between 3 and 254 characters.",
- timeout: 8000
- });
- if (
- email.indexOf("@") !== email.lastIndexOf("@") ||
- !validation.regex.emailSimple.test(email)
- )
- return new Toast({
- content: "Invalid email format.",
- timeout: 8000
- });
- return this.socket.emit(
- "users.updateEmail",
- this.userId,
- email,
- res => {
- if (res.status !== "success")
- new Toast({ content: res.message, timeout: 8000 });
- else {
- new Toast({
- content: "Successfully changed email address",
- timeout: 4000
- });
- this.originalUser.email.address = email;
- }
- }
- );
- },
- changeUsername() {
- const { username } = this.user;
- if (!validation.isLength(username, 2, 32))
- return new Toast({
- content: "Username must have between 2 and 32 characters.",
- timeout: 8000
- });
- if (!validation.regex.azAZ09_.test(username))
- return new Toast({
- content:
- "Invalid username format. Allowed characters: a-z, A-Z, 0-9 and _.",
- timeout: 8000
- });
- return this.socket.emit(
- "users.updateUsername",
- this.userId,
- username,
- res => {
- if (res.status !== "success")
- new Toast({ content: res.message, timeout: 8000 });
- else {
- new Toast({
- content: "Successfully changed username",
- timeout: 4000
- });
- this.originalUser.username = username;
- }
- }
- );
- },
- changeName() {
- const { name } = this.user;
- if (!validation.isLength(name, 1, 64))
- return new Toast({
- content: "Name must have between 1 and 64 characters.",
- timeout: 8000
- });
- return this.socket.emit(
- "users.updateName",
- this.userId,
- name,
- res => {
- if (res.status !== "success")
- new Toast({ content: res.message, timeout: 8000 });
- else {
- new Toast({
- content: "Successfully changed name",
- timeout: 4000
- });
- this.originalUser.name = name;
- }
- }
- );
- },
- changeLocation() {
- const { location } = this.user;
- if (!validation.isLength(location, 0, 50))
- return new Toast({
- content: "Location must have between 0 and 50 characters.",
- timeout: 8000
- });
- return this.socket.emit(
- "users.updateLocation",
- this.userId,
- location,
- res => {
- if (res.status !== "success")
- new Toast({ content: res.message, timeout: 8000 });
- else {
- new Toast({
- content: "Successfully changed location",
- timeout: 4000
- });
- this.originalUser.location = location;
- }
- }
- );
- },
- changeBio() {
- const { bio } = this.user;
- if (!validation.isLength(bio, 0, 200))
- return new Toast({
- content: "Bio must have between 0 and 200 characters.",
- timeout: 8000
- });
- return this.socket.emit(
- "users.updateBio",
- this.userId,
- bio,
- res => {
- if (res.status !== "success")
- new Toast({ content: res.message, timeout: 8000 });
- else {
- new Toast({
- content: "Successfully changed bio",
- timeout: 4000
- });
- this.originalUser.bio = bio;
- }
- }
- );
- },
- changeAvatarType() {
- const { type } = this.user.avatar;
- return this.socket.emit(
- "users.updateAvatarType",
- this.userId,
- type,
- res => {
- if (res.status !== "success")
- new Toast({ content: res.message, timeout: 8000 });
- else {
- new Toast({
- content: "Successfully updated avatar type",
- timeout: 4000
- });
- this.originalUser.avatar.type = type;
- }
- }
- );
- },
- changePassword() {
- const { newPassword } = this;
- if (!validation.isLength(newPassword, 6, 200))
- return new Toast({
- content: "Password must have between 6 and 200 characters.",
- timeout: 8000
- });
- if (!validation.regex.password.test(newPassword))
- return new Toast({
- content:
- "Invalid password format. Must have one lowercase letter, one uppercase letter, one number and one special character.",
- timeout: 8000
- });
- return this.socket.emit(
- "users.updatePassword",
- newPassword,
- res => {
- if (res.status !== "success")
- new Toast({ content: res.message, timeout: 8000 });
- else
- new Toast({
- content: "Successfully changed password",
- timeout: 4000
- });
- }
- );
- },
- requestPassword() {
- return this.socket.emit("users.requestPassword", res => {
- new Toast({ content: res.message, timeout: 8000 });
- if (res.status === "success") {
- this.passwordStep = 2;
- }
- });
- },
- verifyCode() {
- if (!this.passwordCode)
- return new Toast({
- content: "Code cannot be empty",
- timeout: 8000
- });
- return this.socket.emit(
- "users.verifyPasswordCode",
- this.passwordCode,
- res => {
- new Toast({ content: res.message, timeout: 8000 });
- if (res.status === "success") {
- this.passwordStep = 3;
- }
- }
- );
- },
- setPassword() {
- const newPassword = this.setNewPassword;
- if (!validation.isLength(newPassword, 6, 200))
- return new Toast({
- content: "Password must have between 6 and 200 characters.",
- timeout: 8000
- });
- if (!validation.regex.password.test(newPassword))
- return new Toast({
- content:
- "Invalid password format. Must have one lowercase letter, one uppercase letter, one number and one special character.",
- timeout: 8000
- });
- return this.socket.emit(
- "users.changePasswordWithCode",
- this.passwordCode,
- newPassword,
- res => {
- new Toast({ content: res.message, timeout: 8000 });
- }
- );
- },
- unlinkPassword() {
- this.socket.emit("users.unlinkPassword", res => {
- new Toast({ content: res.message, timeout: 8000 });
- });
- },
- unlinkGitHub() {
- this.socket.emit("users.unlinkGitHub", res => {
- new Toast({ content: res.message, timeout: 8000 });
- });
- },
- removeSessions() {
- this.socket.emit(`users.removeSessions`, this.userId, res => {
- new Toast({ content: res.message, timeout: 4000 });
- });
- },
- changeNightmodeLocal() {
- localStorage.setItem("nightmode", this.localNightmode);
- this.changeNightmode(this.localNightmode);
- },
- ...mapActions("user/preferences", ["changeNightmode"])
- }
- };
- </script>
- <style lang="scss" scoped>
- @import "styles/global.scss";
- .container {
- @media only screen and (min-width: 900px) {
- width: 962px;
- margin: 0 auto;
- flex-direction: row;
- .content {
- width: 600px;
- margin-top: 0px;
- }
- }
- margin-top: 32px;
- padding: 24px;
- display: flex;
- flex-direction: column;
- .nav-links {
- height: 100%;
- width: 250px;
- margin-right: 64px;
- a {
- outline: none;
- border: none;
- box-shadow: none;
- color: $musareBlue;
- font-size: 22px;
- line-height: 26px;
- padding: 7px 0 7px 12px;
- width: 100%;
- text-align: left;
- cursor: pointer;
- border-radius: 5px;
- background-color: transparent;
- display: inline-block;
- &.active {
- color: $white;
- background-color: $musareBlue;
- }
- }
- }
- .content {
- margin: 24px 0;
- .control {
- margin-bottom: 24px;
- }
- label {
- font-size: 14px;
- color: $dark-grey-2;
- padding-bottom: 4px;
- }
- input {
- height: 32px;
- }
- textarea {
- height: 96px;
- }
- input,
- textarea {
- border-radius: 3px;
- border: 1px solid $light-grey-2;
- }
- button {
- width: 100%;
- }
- .checkbox-control {
- input[type="checkbox"] {
- opacity: 0;
- position: absolute;
- }
- label {
- display: flex;
- flex-direction: row;
- align-items: center;
- span {
- cursor: pointer;
- width: 24px;
- height: 24px;
- background-color: $white;
- display: inline-block;
- border: 1px solid $dark-grey-2;
- position: relative;
- border-radius: 3px;
- }
- p {
- margin-left: 10px;
- }
- }
- input[type="checkbox"]:checked + label span::after {
- content: "";
- width: 18px;
- height: 18px;
- left: 2px;
- top: 2px;
- border-radius: 3px;
- background-color: $musareBlue;
- position: absolute;
- }
- }
- }
- }
- .avatar-select {
- display: flex;
- flex-direction: column;
- align-items: flex-start;
- .select:after {
- border-color: $musareBlue;
- }
- }
- .night-mode {
- label {
- color: #ddd !important;
- }
- }
- </style>
|